public SQLiteCacheRepositoryTests() { _contextFactory = () => new CacheContext(); _cache = new SQLiteCacheRepository(_contextFactory); _cache.ClearAllAsync().Wait(); }
public CatController( IAsyncCacheRepository cacheRepository, Func <ICatRepository> catRepositoryFactory) { _cacheRepository = cacheRepository; _catRepositoryFactory = catRepositoryFactory; }
/// <summary> /// Get or set by type. /// Cache key is automagically created from object type and identify. /// Example: repo.GetOrSetByType{User}(1, LoadUserByIdFromDb(1)); // Get or load and set User 1 /// </summary> /// <typeparam name="T">Type of cached object</typeparam> /// <param name="repo">IAsyncCacheRepository</param> /// <param name="identifier">Type specific unique identify for object</param> /// <param name="loader">Delegate to invoke if cached item is not found</param> /// <param name="sliding">Sliding expiration to use if object is loaded and cached</param> /// <param name="cancelToken">Cancellation Token</param> /// <returns>Cached object or result of loader</returns> public static Task <T> GetOrSetByTypeAsync <T>(this IAsyncCacheRepository repo, object identifier, Func <Task <T> > loader, Func <T, TimeSpan> sliding, CancellationToken cancelToken = default(CancellationToken)) { var key = CreateKey <T>(identifier); return(repo.GetOrSetAsync(key, loader, sliding, cancelToken)); }
/// <summary> /// Remove by type. /// Cache key is automagically created from object type and identify. /// Example: repo.RemoveByType{User}(1); // Removes user 1 /// </summary> /// <typeparam name="T">Type of cached object</typeparam> /// <param name="repo">IAsyncCacheRepository</param> /// <param name="identifier">Type specific unique identify for object</param> /// <param name="cancelToken">Cancellation Token</param> /// <returns>Task</returns> public static Task RemoveByTypeAsync <T>(this IAsyncCacheRepository repo, object identifier, CancellationToken cancelToken = default(CancellationToken)) { var key = CreateKey <T>(identifier); return(repo.RemoveAsync(key, cancelToken)); }
/// <summary> /// Set by type. /// Cache key is automagically created from object type and identify. /// Cache sliding expiration is taken from the enum value or override by application configuration. /// Example: repo.SetByType{User}(1, user); // Set user by their Id /// </summary> /// <typeparam name="T">Type of cached object</typeparam> /// <param name="repo">IAsyncCacheRepository</param> /// <param name="identifier">Type specific unique identify for object</param> /// <param name="sliding">Sliding expiration to use for cache</param> /// <param name="value">Object to be cached</param> /// <param name="cancelToken">Cancellation Token</param> /// <returns>Task</returns> public static Task SetByTypeAsync <T>(this IAsyncCacheRepository repo, object identifier, T value, CacheSliding sliding, CancellationToken cancelToken = default(CancellationToken)) { var key = CreateKey <T>(identifier); return(repo.SetAsync(key, value, sliding, cancelToken)); }