/// <summary> /// 将缓存项持久化到设备 /// </summary> /// <param name="cacheItem"></param> protected virtual void SaveCacheItem( CacheItem cacheItem ) { using ( var stream = CreateSaveStream( cacheItem.CacheToken ) ) { Serialize( cacheItem, stream ); } }
/// <summary> /// 插入缓存项 /// </summary> /// <param name="cacheItem"></param> public virtual void InsertCacheItem( CacheItem cacheItem ) { if ( !cacheItem.IsValid() )//缓存已过期 return; SaveCacheItem( cacheItem ); }
/// <summary> /// 序列化缓存项 /// </summary> /// <param name="cacheItem"></param> /// <param name="stream"></param> protected virtual void Serialize( CacheItem cacheItem, Stream stream ) { var formatter = new BinaryFormatter(); formatter.Serialize( stream, cacheItem ); }
/// <summary> /// 插入缓存项 /// </summary> /// <param name="cacheItem"></param> public override void InsertCacheItem( CacheItem cacheItem ) { if ( EnableMemoryCache ) HttpRuntime.Cache.InsertCacheItem( cacheItem ); base.InsertCacheItem( cacheItem ); }
/// <summary> /// 获取 CacheItem /// </summary> /// <returns></returns> public CacheItem GetCacheItem() { if ( _cacheItem == null ) _cacheItem = CacheStorageProvider.GetCacheItem( CacheToken ); if ( _cacheItem != null && _cacheItem.IsValid() )//确保缓存未过期 return _cacheItem; return null; }