缓存代理服务器
我正在构建一个缓存代理服务器,我想知道是否有人可以帮助我处理缓存。我使用的是 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 );
}
}
在这里,我在我的 Client 类中调用它:
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( );
}
任何帮助都可以。
I'm building a cache proxy server and i was wondering if someone could help me with the cache. I'm using c#, its just a console application and my cache doesn't seem to work. A tab in a browser will be the client to the proxy server and there should be only a single cache for every client (tab) to access it.
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 );
}
}
And here i'm calling it in my Client class:
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( );
}
Any help would be fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论