/// <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> /// <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> /// <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 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)); }