101 lines
2.6 KiB
C#
101 lines
2.6 KiB
C#
using Asp.Versioning;
|
|
using GerstITS.Examples.Api.Versioning;
|
|
using GerstITS.System.Caching;
|
|
using GerstITS.Validation;
|
|
using GerstITS.Web.Api;
|
|
using GerstITS.Web.Api.FluentApi;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace GerstITS.Examples.Api.Controllers._1._1;
|
|
|
|
/// <summary>
|
|
/// Is responsible to demonstrate the encrypted session based cache.
|
|
/// </summary>
|
|
[ApiController,
|
|
ApiVersion(Versions._1_1),
|
|
Route(ApplicationEnvironment.WebApi.ControllerRouteTemplate)]
|
|
public class CacheController : FluentApiControllerBase
|
|
{
|
|
#region Fields
|
|
|
|
private readonly ICache _cache;
|
|
|
|
#endregion
|
|
|
|
#region Constructors
|
|
|
|
public CacheController(ICache cache,
|
|
IValidator validator)
|
|
: base(validator)
|
|
{
|
|
_cache = cache;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Methods
|
|
|
|
/// <summary>
|
|
/// Gets a cached value by specified key.
|
|
/// </summary>
|
|
/// <param name="key">Key of the cached value.</param>
|
|
/// <returns>Returns the cached value.</returns>
|
|
[HttpGet,
|
|
Route("{key}")]
|
|
public IActionResult Get(string key)
|
|
{
|
|
return Api().Use(key)
|
|
.Get(_cache.Get<string>);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks whether a value is cached for the specified key.
|
|
/// </summary>
|
|
/// <param name="key">Key of the cached value.</param>
|
|
/// <returns>Returns true if a value is cached.</returns>
|
|
[HttpGet,
|
|
Route("{key}/Exists")]
|
|
public IActionResult Exists(string key)
|
|
{
|
|
return Api().Use(key)
|
|
.Get(_cache.ContainsKey);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Caches a value for the specified key.
|
|
/// </summary>
|
|
/// <param name="key">Key of the value to cache.</param>
|
|
/// <param name="value">Value to cache.</param>
|
|
/// <returns>Returns the cached value.</returns>
|
|
[HttpPost,
|
|
Route("{key}")]
|
|
public IActionResult Set(string key, [FromBody] string value)
|
|
{
|
|
return Api().Use(key)
|
|
.Create(x => {
|
|
_cache.Set(x, value);
|
|
|
|
return value;
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes a cached value by specified key.
|
|
/// </summary>
|
|
/// <param name="key">Key of the cached value.</param>
|
|
/// <returns>Returns true if the value was removed.</returns>
|
|
[HttpDelete,
|
|
Route("{key}")]
|
|
public IActionResult Remove(string key)
|
|
{
|
|
return Api().Use(key)
|
|
.Delete(x => {
|
|
_cache.Remove(x);
|
|
|
|
return true;
|
|
});
|
|
}
|
|
|
|
#endregion
|
|
}
|