Esempio n. 1
0
        public Task SetAsync <T>(string key, T value, CacheSliding sliding, CancellationToken cancelToken)
        {
            var min      = _cacheSettings.GetMinutes(sliding);
            var timeSpan = TimeSpan.FromMinutes(min);

            return(SetAsync(key, value, null, timeSpan, cancelToken));
        }
Esempio n. 2
0
 public Task <T> GetOrSetAsync <T>(string key, Func <Task <T> > loader, CacheSliding sliding, CancellationToken cancelToken)
 {
     return(GetOrSetAsync(key, loader, null, v =>
     {
         var min = _cacheSettings.GetMinutes(sliding);
         return TimeSpan.FromMinutes(min);
     }, cancelToken));
 }
Esempio n. 3
0
        /// <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">ICacheRepository</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>
        public static void SetByType <T>(this ICacheRepository repo, object identifier, T value, CacheSliding sliding)
        {
            var key = AsyncCacheRepositoryExtensions.CreateKey <T>(identifier);

            repo.Set(key, value, sliding);
        }
 public int GetMinutes(CacheSliding sliding)
 {
     return((int)sliding);
 }
        /// <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));
        }
Esempio n. 6
0
        /// <summary>
        /// Get or 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.GetOrSetByType{User}(1, LoadUserByIdFromDb(1)); // Get or load and set User 1
        /// </summary>
        /// <typeparam name="T">Type of cached object</typeparam>
        /// <param name="repo">ICacheRepository</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>
        /// <returns>Cached object or result of loader</returns>
        public static T GetOrSetByType <T>(this ICacheRepository repo, object identifier, Func <T> loader, CacheSliding sliding)
        {
            var key = AsyncCacheRepositoryExtensions.CreateKey <T>(identifier);

            return(repo.GetOrSet(key, loader, sliding));
        }
        /// <summary>
        /// Get or 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.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, CacheSliding sliding, CancellationToken cancelToken = default(CancellationToken))
        {
            var key = CreateKey <T>(identifier);

            return(repo.GetOrSetAsync(key, loader, sliding, cancelToken));
        }
 public int GetMinutes(CacheSliding sliding)
 {
     return(_slidingMap.GetOrAdd(sliding, s => GetMinutes(s, (int)s)));
 }
Esempio n. 9
0
        void ICacheRepository.Set <T>(string key, T value, CacheSliding sliding)
        {
            var task = SetAsync(key, value, sliding, CancellationToken.None);

            task.Wait();
        }
Esempio n. 10
0
        T ICacheRepository.GetOrSet <T>(string key, Func <T> loader, CacheSliding sliding)
        {
            var task = GetOrSetAsync(key, () => Task.FromResult(loader()), sliding, CancellationToken.None);

            return(task.Result);
        }
        public void Sliding(CacheSliding sliding, int?expected)
        {
            var actual = ConfigurationCacheSettings.Instance.GetMinutes(sliding);

            Assert.Equal(expected ?? (int)sliding, actual);
        }