mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
Importing the Contrib.Cache module
--HG-- branch : 1.x
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Orchard.OutputCache.Models;
|
||||
using Orchard;
|
||||
using Orchard.Environment.Configuration;
|
||||
|
||||
namespace Orchard.OutputCache.Services {
|
||||
public class DefaultCacheStorageProvider : IOutputCacheStorageProvider {
|
||||
private readonly string _tenantName;
|
||||
private readonly WorkContext _workContext;
|
||||
|
||||
public DefaultCacheStorageProvider(IWorkContextAccessor workContextAccessor, ShellSettings shellSettings) {
|
||||
_workContext = workContextAccessor.GetContext();
|
||||
_tenantName = shellSettings.Name;
|
||||
}
|
||||
|
||||
public void Set(string key, CacheItem cacheItem) {
|
||||
_workContext.HttpContext.Cache.Add(
|
||||
key,
|
||||
cacheItem,
|
||||
null,
|
||||
System.Web.Caching.Cache.NoAbsoluteExpiration,
|
||||
new TimeSpan(0, 0, cacheItem.ValidFor),
|
||||
System.Web.Caching.CacheItemPriority.Normal,
|
||||
null);
|
||||
}
|
||||
|
||||
public void Remove(string key) {
|
||||
_workContext.HttpContext.Cache.Remove(key);
|
||||
}
|
||||
|
||||
public void RemoveAll() {
|
||||
var items = GetCacheItems(0, 100).ToList();
|
||||
while (items.Any()) {
|
||||
foreach (var item in items) {
|
||||
Remove(item.CacheKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public CacheItem GetCacheItem(string key) {
|
||||
return _workContext.HttpContext.Cache.Get(key) as CacheItem;
|
||||
}
|
||||
|
||||
public IEnumerable<CacheItem> GetCacheItems(int skip, int count) {
|
||||
// the ASP.NET cache can also contain other types of items
|
||||
return _workContext.HttpContext.Cache.AsParallel()
|
||||
.Cast<DictionaryEntry>()
|
||||
.Select(x => x.Value)
|
||||
.OfType<CacheItem>()
|
||||
.Where(x => x.Tenant.Equals(_tenantName, StringComparison.OrdinalIgnoreCase))
|
||||
.Skip(skip)
|
||||
.Take(count);
|
||||
}
|
||||
|
||||
public int GetCacheItemsCount() {
|
||||
return _workContext.HttpContext.Cache.AsParallel()
|
||||
.Cast<DictionaryEntry>()
|
||||
.Select(x => x.Value)
|
||||
.OfType<CacheItem>()
|
||||
.Count(x => x.Tenant.Equals(_tenantName, StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user