ASP.NET Core Uso de sesiones
Una vez que haya configurado NCache como caché predeterminado para ASP.NET Core Sesiones, puedes realizar todas las ASP..NET Core Operaciones específicas de sesiones sin ningún cambio de código. Todas las sesiones se almacenarán en caché en modo distribuido. NCache.
El siguiente ejemplo de código busca la clave de caché en el caché y, si la encuentra, devuelve la vista con el resultado. De lo contrario, agrega la clave de caché a su entrada de caché correspondiente y establece la caducidad deslizante de la entrada de caché en 30 minutos.
Para más detalles sobre IDistributedCache
métodos, echa un vistazo a su documentación.
Uso de ASP.NET Core Sesiones para un único caché
Para usar ASP.NET Core Actualización de sesiones InicioController.cs de su aplicación:
public class HomeController : Controller
{
private IDistributedCache _cache;
public HomeController(IDistributedCache Cache)
{
// Underlying cache is NCache
_cache = Cache;
}
public IActionResult CacheOperations()
{
// Cache entry is of DateTime type
DateTime cacheEntry;
string cacheKey = "MaxValue: " + DateTime.MaxValue;
// Look for cache key
object retrieveObj = _cache.Get(cacheKey);
// Check if key exists in cache
if (retrieveObj != null)
{
// Return view with result
}
else
{
// Key not in cache, so populate data in cache entry
cacheEntry = DateTime.MaxValue;
// Configure SlidingExpiration for item
var cacheEntryOptions = new DistributedCacheEntryOptions();
cacheEntryOptions.SlidingExpiration = TimeSpan.FromMinutes(30);
// Insert item in cache
_cache.Set(cacheKey, new byte[1024], cacheEntryOptions);
// Return view with cacheEntry
}
}
}
Uso de ASP.NET Core Sesiones para múltiples cachés
Note
Esta función está disponible en NCache Enterprise solamente.
public class HomeController : Controller
{
private IDistributedCacheFactory _cache;
public HomeController(IDistributedCacheFactory cacheFactory)
{
// Underlying cache is NCache
_cache = cacheFactory.GetDistributedCache("demoClusteredCache");
}
public IActionResult CacheOperations()
{
// Cache entry is of DateTime type
DateTime cacheEntry;
string cacheKey = "MaxValue: " + DateTime.MaxValue;
// Look for cache key
object retrieveObj = _cache.Get(cacheKey);
// Check if key exists in cache
if (retrieveObj != null)
{
// Return view with result
}
else
{
// Key not in cache, so populate data in cache entry
cacheEntry = DateTime.MaxValue;
// Configure SlidingExpiration for item
var cacheEntryOptions = new DistributedCacheEntryOptions();
cacheEntryOptions.SlidingExpiration = TimeSpan.FromMinutes(30);
// Insert item in cache
_cache.Set(cacheKey, new byte[1024], cacheEntryOptions);
// Return view with cacheEntry
}
}
}
Recursos adicionales
NCache proporciona una aplicación de muestra para el almacenamiento en caché de sesiones en GitHub.
Vea también
.NETO: Alachisoft.NCache.Caching.Distribuido espacio de nombres