我正在构建一个缓存代理服务器,我想知道是否有人可以帮助我缓存。我正在使用c#,它只是一个控制台应用程序,我的缓存似乎不起作用。浏览器中的选项卡将是代理服务器的客户端,每个客户端(选项卡)应该只有一个缓存来访问它。
public class CacheManager
{
private static CacheManager _instance;
private static readonly object _syncLock = new object( );
private static ObjectCache _currentCache = MemoryCache.Default;
private CacheManager( )
{
}
public static CacheManager Instance
{
get
{
lock ( _syncLock )
{
if ( _instance == null )
_instance = new CacheManager( );
}
return _instance;
}
}
public bool Contains( string key )
{
return _currentCache[ key ] != null;
}
public T Get<T>( string key )
{
return ( T )_currentCache[ key ];
}
public void Add<T>( string key, T data ) where T : class
{
if ( data != null )
{
var cachePolicy = new CacheItemPolicy
{
AbsoluteExpiration = DateTime.Now.AddHours( 1 ),
SlidingExpiration = TimeSpan.Zero
};
_currentCache.Add( new CacheItem( key, data ), cachePolicy );
}
}
public void Remove( string key )
{
if ( !Contains( key ) ) return;
_currentCache.Remove( key );
}
}
在这里,我在我的客户端类中调用它:
if( cache.Contains( _host ) )
{
//Console.WriteLine( "Object in cache! :D" );
var get = ( String )cache.Get<String>( _host );
_cacheBuffer = Encoding.ASCII.GetBytes( get );
ClientSocket.Send( _cacheBuffer );
}
else
{
Action inv = ( ) => StartTransferToBrowser( );
inv.Invoke( );
}
任何帮助都没问题。