public void AddCaching(Type caching, ICachingOptions cachingOptions) { UraganoSettings.CachingOptions = cachingOptions; ServiceCollection.AddSingleton(typeof(ICaching), caching); ServiceCollection.AddSingleton(typeof(ICachingKeyGenerator), cachingOptions.KeyGenerator); RegisterSingletonService <CachingDefaultInterceptor>(); }
/// <summary> /// Caches an object /// </summary> /// <param name="key">A unique key to cache the object under</param> /// <param name="obj">The object to cache</param> /// <param name="cachingOptions">Options on how to cache</param> public static void Add(string key, object obj, ICachingOptions cachingOptions) { if (string.IsNullOrEmpty(key)) { throw new ArgumentNullException("key"); } if (obj == null) { throw new ArgumentNullException("obj"); } if (cachingOptions == null) { throw new ArgumentNullException("cachingOptions"); } CacheItemPolicy cacheItemPolicy = new CacheItemPolicy { Priority = CacheItemPriority.Default, AbsoluteExpiration = DateTime.Now.AddMilliseconds(cachingOptions.Expiration.TotalMilliseconds) }; if (cachingOptions.OnRemoved != null) { cacheItemPolicy.RemovedCallback += x => cachingOptions.OnRemoved(x.RemovedReason); } CacheLock.EnterWriteLock(); Cache.Set(key, obj, cacheItemPolicy); CacheLock.ExitWriteLock(); }
public MemoryCache(IMemoryCache cache, ICachingOptions cachingOptions) { this.Cache = cache; this.CachingOptions = cachingOptions; _keys = new List <string>(); }
public void Execute(CallbackArgs args) { IPublish <Message> response = args.Publish; ISubscription subscription = args.Peer.GetSubscription(response.Key); if (subscription == null) { return; } ICachingOptions cachingOptions = subscription.CachingOptions; if (cachingOptions == null || !cachingOptions.EnableCaching) { return; } if (!cachingOptions.HasCacheKey) { return; } string callbackKey = string.Format("callback#{0}#{1}", args.Peer.Node.Contact.NodeId, response.Key); string cacheKey = callbackKey; if (string.Equals(cachingOptions.Key, response.Key)) { string extraKey = cachingOptions.GetCacheKey(response.Message); if (!string.IsNullOrEmpty(extraKey)) { cacheKey = string.Format("{0}#{1}", callbackKey, extraKey); } } CacheManager.Add(cacheKey, args.Publish.Message, cachingOptions); // Request response scheme if (string.Equals(subscription.Key, cachingOptions.Key)) { return; } IPublish <Message> request = CacheManager.Get <IPublish <Message> >(string.Format("publish#{0}#{1}", args.Peer.Node.Contact.NodeId, cachingOptions.Key)); if (request != null && string.Equals(request.Key, cachingOptions.Key)) { string extraKey = cachingOptions.GetCacheKey(request.Message); if (!string.IsNullOrEmpty(extraKey)) { cacheKey = string.Format("{0}#{1}", callbackKey, extraKey); CacheManager.Add(cacheKey, args.Publish.Message, cachingOptions); } } }
/// <summary> /// Retrieves an item from the cache and adds it if is not there /// </summary> /// <param name="key"></param> /// <param name="selector"></param> /// <param name="cachingOptions"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> /// <exception cref="ArgumentNullException"></exception> public static T GetOrAdd <T>(string key, Func <T> selector, ICachingOptions cachingOptions) where T : class { if (string.IsNullOrEmpty(key)) { throw new ArgumentNullException("key"); } if (selector == null) { throw new ArgumentNullException("selector"); } if (cachingOptions == null) { throw new ArgumentNullException("cachingOptions"); } T value = Get <T>(key); if (value != null) { return(value); } CacheItemPolicy cacheItemPolicy = new CacheItemPolicy { Priority = CacheItemPriority.Default, AbsoluteExpiration = DateTime.Now.AddMilliseconds(cachingOptions.Expiration.TotalMilliseconds) }; if (cachingOptions.OnRemoved != null) { cacheItemPolicy.RemovedCallback += x => cachingOptions.OnRemoved(x.RemovedReason); } try { CacheLock.EnterWriteLock(); value = Cache[key] as T; if (value != null) { return(value); } value = selector(); Cache.Set(key, value, cacheItemPolicy); return(value); } finally { CacheLock.ExitWriteLock(); } }
public void AddCaching <TCaching>(ICachingOptions cachingOptions) where TCaching : ICaching { AddCaching(typeof(TCaching), cachingOptions); }
public void AddCaching(Type caching, ICachingOptions cachingOptions) { AddCaching(caching, typeof(CachingKeyGenerator), cachingOptions); }
public void AddCaching <TCaching, TKeyGenerator>(ICachingOptions cachingOptions) where TCaching : class, ICaching where TKeyGenerator : class, ICachingKeyGenerator { AddCaching(typeof(TCaching), typeof(TKeyGenerator), cachingOptions); }