我正在使用包裹在微缓存中的对象缓存,有人在此处发布了...
无论我在CacheItemPolicy中将到期时间设置为什么,我都无法刷新它...
我调用flush方法...仍然获得相同的数据,通过appsettings关闭缓存,并显示新行...
重新打开缓存,没有新行...反弹iis或AppPool ..没有新行(这确实让我感到困惑)
有什么想法吗?
这是我的实现方式
private static IMicroCache< List<DDLDispValueCV> > pocsFromCommonCtrLvlOusCache = new MicroCache< List<DDLDispValueCV> >( System.Runtime.Caching.MemoryCache.Default ); // see comments at cache methods for more detail - EWB
通话
public List<DDLDispValueCV> GetAllPocsFromCommonCtrLvlOus( string sAdName )
{
if ( CacheIsOn() )// controlled via appsetting
{
return pocsFromCommonCtrLvlOusCache.GetOrAdd(
sAdName, // The key in the cache - EWB
() => GetAllPocsFromCommonCtrLvlOusUncached( sAdName ), // called to fetch data if not in cache - EWWB
() => MicroCachePolicies.SimpleDurationLoadCacheItemPolicy( 1 ) // cache for 1 min of inactivity... - EWB
);
}
else
{
return GetAllPocsFromCommonCtrLvlOusUncached( sAdName );
}
}
基本MicroCache
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
using System.Collections;
using System.Diagnostics;
using System.Runtime.Caching;
using System.Threading;
namespace JCAP.BLL.BusinessObjects
{
public interface IMicroCache<T>
{
bool Contains( string key );
T GetOrAdd( string key, Func<T> loadFunction, Func<CacheItemPolicy> getCacheItemPolicyFunction );
void Remove( string key );
void Flush();
}
public class MicroCache<T> : IMicroCache<T>
{
public MicroCache( ObjectCache objectCache )
{
if ( objectCache == null )
throw new ArgumentNullException( "objectCache" );
this.cache = objectCache;
}
public void Flush()
{
synclock.EnterUpgradeableReadLock();
synclock.EnterWriteLock();
try
{
//Int64 cnt = cache.GetCount();
//for ( Int64 i=0 ; i < cnt ; i++ )
//{
// cache.Remove( cache.First().Key );
//}
//List<string> keys = new List<string>();
//IDictionaryEnumerator enumerator = cache.;
//while ( enumerator.MoveNext() )
//{
// keys.Add( enumerator.Key.ToString() );
//}
//for ( int i = 0 ; i < keys.Count ; i++ )
//{
// cache.Remove( keys[ i ] );
//}
List<string> cacheKeys = cache.Select(kvp => kvp.Key).ToList();
foreach ( string cacheKey in cacheKeys )
{
cache.Remove( cacheKey );
}
}
catch ( Exception ex )
{
Trace.WriteLine( ex.ToString() );
throw;
}
finally
{
synclock.ExitUpgradeableReadLock();
synclock.ExitWriteLock();
}
}
public virtual string KeyWork( string key )
{
return key;
}
private readonly ObjectCache cache;
private ReaderWriterLockSlim synclock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
public bool Contains( string key )
{
key = KeyWork( key );
synclock.EnterReadLock();
try
{
return this.cache.Contains( key );
}
catch ( Exception ex )
{
Trace.WriteLine( ex.ToString() );
throw;
}
finally
{
synclock.ExitReadLock();
}
}
public T GetOrAdd( string key, Func<T> loadFunction, Func<CacheItemPolicy> getCacheItemPolicyFunction )
{
key = KeyWork( key );
LazyLock<T> lazy;
bool success = false;
try
{
synclock.EnterReadLock();
try
{
success = this.TryGetValue( key, out lazy );
}
catch ( Exception ex )
{
Trace.WriteLine( ex.ToString() );
throw;
}
finally
{
synclock.ExitReadLock();
}
if ( !success )
{
synclock.EnterWriteLock();
try
{
if ( !this.TryGetValue( key, out lazy ) )
{
lazy = new LazyLock<T>();
var policy = getCacheItemPolicyFunction();
this.cache.Add( key, lazy, policy );
}
}
catch ( Exception ex )
{
Trace.WriteLine( ex.ToString() );
throw;
}
finally
{
synclock.ExitWriteLock();
}
}
}
catch ( Exception ex )
{
lazy = new LazyLock<T>();
}
return lazy.Get( loadFunction );
}
public void Remove( string key )
{
key = KeyWork( key );
synclock.EnterWriteLock();
try
{
this.cache.Remove( key );
}
catch( Exception ex )
{
Trace.WriteLine( ex.ToString() );
throw;
}
finally
{
synclock.ExitWriteLock();
}
}
private bool TryGetValue( string key, out LazyLock<T> value )
{
key = KeyWork( key );
try
{
value = ( LazyLock<T> ) this.cache.Get( key );
if ( value != null )
{
return true;
}
return false;
}
catch ( Exception ex )
{
throw;
}
}
private sealed class LazyLock<T>
{
private volatile bool got;
private T value;
public T Get( Func<T> activator )
{
if ( !got )
{
if ( activator == null )
{
return default( T );
}
lock ( this )
{
if ( !got )
{
value = activator();
got = true;
}
}
}
return value;
}
}
}
public class MicroCachePolicies
{
public static CacheItemPolicy SimpleDurationLoadCacheItemPolicy( int min = 5 )
{
var policy = new CacheItemPolicy();
// This ensures the cache will survive application
// pool restarts in ASP.NET/MVC
policy.Priority = CacheItemPriority.Default;//CacheItemPriority.NotRemovable;
policy.AbsoluteExpiration = DateTimeOffset.Now.AddMinutes( min );
// Load Dependencies
// policy.ChangeMonitors.Add(new HostFileChangeMonitor(new string[] { fileName }));
return policy;
}
}
}