public void GetUserWorkspaces_WorkspacesExist_CachesResult() { string authUserId = Guid.NewGuid().ToString(); string cacheKey = CacheKeys.UserWorkspace(authUserId); IDbContext dbContext = Substitute.For <IDbContext>(); IEnumerable <WorkspaceViewModel>?cachedViewModel = null; UserDbModel userDbModel = new UserDbModel(); userDbModel.Workspaces = new string[1] { "workspace1" }; WorkspaceDbModel[] workspaceDbModels = new WorkspaceDbModel[1] { new WorkspaceDbModel() }; UserRepository userRepository = Substitute.For <UserRepository>(); userRepository.GetByAuthIdAsync(dbContext, authUserId).Returns <UserDbModel>(userDbModel); WorkspaceRepository workspaceRepository = Substitute.For <WorkspaceRepository>(); workspaceRepository.GetManyByIdAsync(dbContext, userDbModel.Workspaces).Returns(workspaceDbModels); IMemoryCacheWrapper memoryCache = Substitute.For <IMemoryCacheWrapper>(); memoryCache.Get <IEnumerable <WorkspaceViewModel> >(cacheKey).Returns(cachedViewModel); UserWorkspaceViewService userWorkspaceViewService = new UserWorkspaceViewService(dbContext, memoryCache, Substitute.For <IMapper>(), userRepository, workspaceRepository); IEnumerable <WorkspaceViewModel> result = userWorkspaceViewService.GetUserWorkspaces(authUserId).Result; // assert memoryCache.Received(1).Set <IEnumerable <WorkspaceViewModel> >(cacheKey, Arg.Any <IEnumerable <WorkspaceViewModel> >(), Arg.Any <TimeSpan>()); }
public void GetUserWorkspaces_NoUser_ReturnsEmptyEnumerable() { string authUserId = Guid.NewGuid().ToString(); string cacheKey = CacheKeys.UserWorkspace(authUserId); IDbContext dbContext = Substitute.For <IDbContext>(); IEnumerable <WorkspaceViewModel>?cachedViewModel = null; UserRepository userRepository = Substitute.For <UserRepository>(); userRepository.GetByAuthIdAsync(dbContext, authUserId).Returns <UserDbModel>(x => null); WorkspaceRepository workspaceRepository = Substitute.For <WorkspaceRepository>(); IMemoryCacheWrapper memoryCache = Substitute.For <IMemoryCacheWrapper>(); memoryCache.Get <IEnumerable <WorkspaceViewModel> >(cacheKey).Returns(cachedViewModel); UserWorkspaceViewService userWorkspaceViewService = new UserWorkspaceViewService(dbContext, memoryCache, Substitute.For <IMapper>(), userRepository, workspaceRepository); IEnumerable <WorkspaceViewModel> result = userWorkspaceViewService.GetUserWorkspaces(authUserId).Result; // assert Assert.AreEqual(0, result.Count()); userRepository.Received(1).GetByAuthIdAsync(dbContext, authUserId); workspaceRepository.DidNotReceive().GetManyByIdAsync(Arg.Any <IDbContext>(), Arg.Any <IEnumerable <string> >()); }
public SensorDataService(IApiClient aPIClient, DataContext dataContext, INotificationService notificationService, IMemoryCacheWrapper memoryCacheWrapper) { this.apiClient = aPIClient ?? throw new ArgumentNullException(nameof(aPIClient)); this.dataContext = dataContext ?? throw new ArgumentNullException(nameof(dataContext)); this.notificationService = notificationService ?? throw new ArgumentNullException(nameof(notificationService)); this.memoryCacheWrapper = memoryCacheWrapper ?? throw new ArgumentNullException(nameof(memoryCacheWrapper));; }
public UserWorkspaceViewService(IDbContext dbContext, IMemoryCacheWrapper memoryCache, IMapper mapper, UserRepository userRepository, WorkspaceRepository workspaceRepository) { this._dbContext = dbContext; this._memoryCache = memoryCache; this._mapper = mapper; this._userRepository = userRepository; this._workspaceRepository = workspaceRepository; }
protected BaseLoyalBooksWebApiManager(IWebApiProcessor apiProcessor, IMemoryCacheWrapper cache, IOptions <MyConfig> config) { this.apiProcessor = apiProcessor; this.config = config; //this.apiProcessor.ApiPath = "download/text/"; //this.apiProcessor.WebLocation = "http://www.loyalbooks.com/"; this.apiProcessor.ApiPath = this.config.Value.ApiPath; this.apiProcessor.WebLocation = this.config.Value.HostServerUrl; this.cache = cache; this.cacheEntryOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromHours(1)); }
public CmsTemplate(IMemoryCacheWrapper cache, TemplateNames names) { this.cache = cache; var opt = new Options { EnabledCompress = Settings.TPL_USE_COMPRESS, EnabledCache = true, UrlQueryShared = true, HttpItemShared = true, Names = names, }; registry = new TemplateRegistry(createContainer(), opt); }
/// <summary> /// /// </summary> /// <param name="cache"></param> /// <param name="names"></param> public CmsTemplate(IMemoryCacheWrapper cache, TemplateNames names) { this._cache = cache; var opt = new Options { EnabledCompress = Settings.TPL_USE_COMPRESS, // 非正式环境关闭模板缓存 EnabledCache = Cms.OfficialEnvironment && Settings.TPL_USE_CACHE, UrlQueryShared = true, HttpItemShared = true, Names = names }; _registry = new TemplateRegistry(CreateContainer(), opt); }
public void GetUserWorkspaces_WhenUserWorkspacesCached_DoesNotFetchFromRepository() { string authUserId = Guid.NewGuid().ToString(); string cacheKey = CacheKeys.UserWorkspace(authUserId); IDbContext dbContext = Substitute.For <IDbContext>(); IEnumerable <WorkspaceViewModel>?cachedViewModel = new WorkspaceViewModel[] { new WorkspaceViewModel() }; UserRepository userRepository = Substitute.For <UserRepository>(); WorkspaceRepository workspaceRepository = Substitute.For <WorkspaceRepository>(); IMemoryCacheWrapper memoryCache = Substitute.For <IMemoryCacheWrapper>(); memoryCache.Get <IEnumerable <WorkspaceViewModel> >(cacheKey).Returns(cachedViewModel); UserWorkspaceViewService userWorkspaceViewService = new UserWorkspaceViewService(dbContext, memoryCache, Substitute.For <IMapper>(), userRepository, workspaceRepository); IEnumerable <WorkspaceViewModel> result = userWorkspaceViewService.GetUserWorkspaces(authUserId).Result; // assert memoryCache.DidNotReceive().Set <IEnumerable <WorkspaceViewModel> >(Arg.Any <string>(), Arg.Any <IEnumerable <WorkspaceViewModel> >(), Arg.Any <TimeSpan>()); workspaceRepository.DidNotReceive().GetManyByIdAsync(Arg.Any <IDbContext>(), Arg.Any <IEnumerable <string> >()); userRepository.DidNotReceive().GetByAuthIdAsync(Arg.Any <IDbContext>(), Arg.Any <string>()); }
public void GetUserWorkspaces_UserWorkspacesEmpty_ReturnsEmptyEnumerable() { string authUserId = Guid.NewGuid().ToString(); string cacheKey = CacheKeys.UserWorkspace(authUserId); IDbContext dbContext = Substitute.For <IDbContext>(); IEnumerable <WorkspaceViewModel>?cachedViewModel = null; UserDbModel userDbModel = new UserDbModel(); userDbModel.Workspaces = new string[0]; UserRepository userRepository = Substitute.For <UserRepository>(); userRepository.GetByAuthIdAsync(dbContext, authUserId).Returns <UserDbModel>(userDbModel); IMemoryCacheWrapper memoryCache = Substitute.For <IMemoryCacheWrapper>(); memoryCache.Get <IEnumerable <WorkspaceViewModel> >(cacheKey).Returns(cachedViewModel); UserWorkspaceViewService userWorkspaceViewService = new UserWorkspaceViewService(dbContext, memoryCache, Substitute.For <IMapper>(), userRepository, Substitute.For <WorkspaceRepository>()); IEnumerable <WorkspaceViewModel> result = userWorkspaceViewService.GetUserWorkspaces(authUserId).Result; // assert Assert.AreEqual(0, result.Count()); }
public LoyalBooksWebApiManager(IWebApiProcessor apiProcessor, IMemoryCacheWrapper cache, ITextProcessor textProcessor, IOptions <MyConfig> config) : base(apiProcessor, cache, config) { this.textProcessor = textProcessor; }
public FibonacciService(IMemoryCacheWrapper memoryCacheWrapper) { _memoryCacheWrapper = memoryCacheWrapper; }
private static void PrepareCms() { #if NETSTANDARD IsNetStandard = true; if (_cache == null) { _cache = new MemoryCacheWrapper(); } #endif Version = CmsVariables.VERSION; PhysicPath = EnvUtil.GetBaseDirectory(); //获取编译生成的时间 //DateTime builtDate = new DateTime(2000, 1, 1).AddDays(ver.Build).AddSeconds(ver.Revision*2); var filePath = typeof(Cms).Assembly.Location; if (string.IsNullOrEmpty(filePath)) { filePath = PhysicPath + CmsVariables.FRAMEWORK_ASSEMBLY_PATH + "jrcms.dll"; } var builtDate = File.GetLastWriteTime(filePath); BuiltTime = DateHelper.ToUnix(builtDate); //获取平台 var platFormId = (int)Environment.OSVersion.Platform; if (platFormId == 4 || platFormId == 6 || platFormId == 128) { RunAtMono = true; } //初始化 //todo: plugin //Plugins = new CmsPluginContext(); // 初始化缓存工厂 CmsCacheFactory.Configure(_cache); CacheFactory.Configure(_cache); // 初始化模板 Template = new CmsTemplate(_cache, TemplateNames.FileName); Cache = new CmsCache(CmsCacheFactory.Singleton); // 初始化内存缓存 Utility = new CmsUtility(); Language = new CmsLanguagePackage(); #region 缓存清除 // //UNDONE: 弱引用 // /* * WeakRefCache.OnLinkBuilting += () => * { * Cms.Cache.Clear(CacheSign.Link.ToString()); * }; * * WeakRefCache.OnModuleBuilting += () => * { * Cms.Cache.Clear(CacheSign.Module.ToString()); * }; * * WeakRefCache.OnPropertyBuilting += () => * { * Cms.Cache.Clear(CacheSign.Property.ToString()); * }; * * WeakRefCache.OnTemplateBindBuilting += () => * { * Cms.Cache.Clear(CacheSign.TemplateBind.ToString()); * }; * */ #endregion }
public static void Configure(IMemoryCacheWrapper cache) { _cacheInstance = new BasicCache(new DependCache(cache)); }
/// <summary> /// 设置缓存 /// </summary> /// <param name="cache">缓存包装器</param> public static void Configure(IMemoryCacheWrapper cache) { cacheInstance = new CmsDependCache(cache); }
public TemplateDataAdapter(ICompatibleHttpContext context, IMemoryCacheWrapper cache) { this._context = context; this.cache = cache; }
public static void ConfigCache(IMemoryCacheWrapper cache) { _cache = cache; }
public LangLabelReader(string filePath) { this.filePath = filePath; this.cache = CacheFactory.Sington.RawCache(); cacheKey = String.Format("lang_cache_{0}", this.filePath.GetHashCode()); }
public ReverseWordsService(IMemoryCacheWrapper memoryCacheWrapper) { _memoryCacheWrapper = memoryCacheWrapper; }
internal DependCache(IMemoryCacheWrapper cache) { this._cacheWrapper = cache; }
internal CmsDependCache(IMemoryCacheWrapper cache) { _cacheDependFile = Variables.PhysicPath + "config/cache.pid"; this.cache = cache; }
public TriangleTypeService(IMemoryCacheWrapper memoryCacheWrapper) { _memoryCacheWrapper = memoryCacheWrapper; }