/// <summary> /// 获取或设置缓存对象。 /// </summary> /// <typeparam name="TCache">当前缓存对象类型。</typeparam> /// <param name="key">缓存唯一键。</param> /// <param name="dependency">缓存依赖项。</param> /// <param name="action">获取和配置缓存实例。</param> /// <returns>返回当前缓存对象。</returns> public virtual async Task <TCache> GetOrCreateAsync <TCache>(object key, IStorageCacheDependency dependency, Func <IStorageContext, Task <TCache> > action) { var cache = await GetCacheAsync(key, dependency); if (cache == null) { var cacheKey = GetCacheKey(key); var context = new StorageContext(cacheKey); var instance = await action(context); cache = new StorageCache { CacheKey = cacheKey, Dependency = dependency?.ToString(), ExpiredDate = context.ExpiredDate }; await _db.CreateAsync(cache); await StorageHelper.SaveTextAsync(GetFilePath(cacheKey, true), instance.ToJsonString()); return(instance); } var path = GetFilePath(cache.CacheKey); if (!File.Exists(path))//缓存文件不存在 { await RemoveAsync(cache.CacheKey); return(await GetOrCreateAsync(key, dependency, action)); } return(Cores.FromJsonString <TCache>(await StorageHelper.ReadTextAsync(path))); }
/// <summary> /// 获取或设置缓存对象。 /// </summary> /// <typeparam name="TCache">当前缓存对象类型。</typeparam> /// <param name="key">缓存唯一键。</param> /// <param name="dependency">缓存依赖项。</param> /// <param name="action">获取和配置缓存实例。</param> /// <returns>返回当前缓存对象。</returns> public TCache GetOrCreate <TCache>(object key, IStorageCacheDependency dependency, Func <IStorageContext, TCache> action) { var cache = GetCache(key, dependency); if (cache == null) { var cacheKey = GetCacheKey(key); var context = new StorageContext(cacheKey); var instance = action(context); cache = new StorageCache { CacheKey = cacheKey, Dependency = dependency?.ToString(), ExpiredDate = context.ExpiredDate }; _db.Create(cache); StorageHelper.SaveText(GetFilePath(cacheKey, true), instance.ToJsonString()); return(instance); } var path = GetFilePath(cache.CacheKey); if (!File.Exists(path))//缓存文件不存在 { Remove(cache.CacheKey); return(GetOrCreate(key, dependency, action)); } return(Cores.FromJsonString <TCache>(StorageHelper.ReadText(path))); }
/// <summary> /// 获取或设置缓存对象。 /// </summary> /// <param name="key">缓存唯一键。</param> /// <param name="dependency">缓存依赖项。</param> /// <param name="action">获取和配置缓存实例。</param> /// <returns>返回当前缓存对象。</returns> public virtual async Task <string> GetOrCreateAsync(object key, IStorageCacheDependency dependency, Func <IStorageContext, Task <string> > action) { var cache = await GetCacheAsync(key, dependency); if (cache == null) { var cacheKey = GetCacheKey(key); var context = new StorageContext(cacheKey); var text = await action(context); cache = new StorageCache { CacheKey = cacheKey, Dependency = dependency?.ToString(), ExpiredDate = context.ExpiredDate }; await _db.CreateAsync(cache); await StorageHelper.SaveTextAsync(GetFilePath(cacheKey, true), text, FileShare.Read); return(text); } var path = GetFilePath(cache.CacheKey); if (!File.Exists(path))//缓存文件不存在 { await RemoveAsync(cache.CacheKey); return(await GetOrCreateAsync(key, dependency, action)); } return(await StorageHelper.ReadTextAsync(path, share : FileShare.Read)); }
/// <summary> /// 获取或设置缓存对象。 /// </summary> /// <param name="key">缓存唯一键。</param> /// <param name="dependency">缓存依赖项。</param> /// <param name="action">获取和配置缓存实例。</param> /// <returns>返回当前缓存对象。</returns> public virtual string GetOrCreate(object key, IStorageCacheDependency dependency, Func <IStorageContext, string> action) { var cache = GetCache(key, dependency); if (cache == null) { var cacheKey = GetCacheKey(key); var context = new StorageContext(cacheKey); var text = action(context); cache = new StorageCache { CacheKey = cacheKey, Dependency = dependency?.ToString(), ExpiredDate = context.ExpiredDate }; _db.Create(cache); StorageHelper.SaveText(GetFilePath(cacheKey, true), text, FileShare.Read); return(text); } var path = GetFilePath(cache.CacheKey); if (!File.Exists(path))//缓存文件不存在 { Remove(cache.CacheKey); return(GetOrCreate(key, dependency, action)); } return(StorageHelper.ReadText(path, share: FileShare.Read)); }
/// <summary> /// 获取缓存配置实例。 /// </summary> /// <param name="key">缓存键。</param> /// <param name="dependency">缓存依赖项。</param> /// <returns>返回缓存配置实例。</returns> protected StorageCache GetCache(object key, IStorageCacheDependency dependency) { var hashedKey = GetCacheKey(key); var cache = _cache.GetOrCreate(hashedKey, ctx => { ctx.SetAbsoluteExpiration(Cores.DefaultCacheExpiration); return(_db.Find(hashedKey)); }); if (cache == null) { return(null); } if (dependency?.IsEqual(cache.Dependency) == false || cache.ExpiredDate != null && cache.ExpiredDate < DateTimeOffset.Now) { Remove(hashedKey); return(null); } return(cache); }
/// <summary> /// 获取缓存配置实例。 /// </summary> /// <param name="key">缓存键。</param> /// <param name="dependency">缓存依赖项。</param> /// <returns>返回缓存配置实例。</returns> protected async Task <StorageCache> GetCacheAsync(object key, IStorageCacheDependency dependency) { var hashedKey = GetCacheKey(key); var cache = await _cache.GetOrCreateAsync(hashedKey, async ctx => { ctx.SetAbsoluteExpiration(Cores.DefaultCacheExpiration); return(await _db.FindAsync(hashedKey)); }); if (cache == null) { return(null); } if (dependency?.IsEqual(cache.Dependency) == false || cache.ExpiredDate != null && cache.ExpiredDate < DateTimeOffset.Now) { await RemoveAsync(hashedKey); return(null); } return(cache); }
/// <summary> /// 是否相等。 /// </summary> /// <param name="dependency">缓存依赖项。</param> /// <returns>返回判断结果。</returns> public bool IsEqual(IStorageCacheDependency dependency) { return(IsEqual(dependency.ToString())); }