Example #1
0
 public override object Get(string key)
 {
     lock (_sync)
     {
         return(_cache.Contains(key) ? _cache.Get(key) : null);
     }
 }
 public void Clear(string key)
 {
     if (_cache.Contains(key))
     {
         _cache.Remove(key);
     }
 }
        public Task <bool> IsTracked(Guid id)
        {
#if NET452
            return(Task.FromResult(_cache.Contains(id.ToString())));
#else
            return(Task.FromResult(_cache.TryGetValue(id, out var o) && o != null));
#endif
        }
 public T Get <T>(string key, Func <T> loader)
 {
     if (!_cache.Contains(key))
     {
         _cache.Add(key, loader(), DateTimeOffset.UtcNow.AddSeconds(_config.CacheDurationSeconds));
     }
     return((T)_cache.GetCacheItem(key).Value);
 }
Example #5
0
        public bool IsTracked(Guid id)
        {
#if NET461
            return(_cache.Contains(id.ToString()));
#else
            return(_cache.TryGetValue(id, out var o) && o != null);
#endif
        }
Example #6
0
 /// <summary>
 /// 缓存是否已存在
 /// </summary>
 /// <param name="key">key</param>
 /// <returns></returns>
 public bool Exist(string key)
 {
     if (string.IsNullOrWhiteSpace(key))
     {
         new NullReferenceException("Exist方法的{key}参数值为空。");
     }
     return(cache.Contains(key));
 }
        public Task <bool> IsTracked(string id)
        {
#if NET452
            return(Task.FromResult(_cache.Contains(id)));
#else
            return(Task.FromResult(_cache.TryGetValue(id, out var o)));
#endif
        }
        public bool IsTracked(string id)
        {
            object o;

#if NET451
            return(_cache.Contains(id));
#else
            return(_cache.TryGetValue(id, out o));
#endif
        }
        public string HorasTotales(string nCuenta)
        {
            try
            {
                var checkCache = cache.Contains(nCuenta);
                if (checkCache)
                {
                    var    value = cache.Get(nCuenta);
                    double val   = double.Parse(value.ToString());
                    int    v     = Convert.ToInt32(val);
                    return(v.ToString());
                }
            }
            catch (Exception e)
            {
                Logger.Log("Error Horas totales" + e.Message, LogType.Error);
                Console.WriteLine("cast horas tot");
                Close();
                //return " ";
            }
            string selectQuery = "SELECT sum(Horas_Acum) as Horas_Totales FROM [Tabla General] where No_Cuenta = ? ";

            try
            {
                // Console.WriteLine("nUMEROcUENTA " + nCuenta);
                int NumeroTotalHoras = 0;
                cmd = new OdbcCommand(selectQuery, odbcConnection);
                Open();
                try
                {
                    cmd.Parameters.Add("@Cuenta", OdbcType.VarChar).Value = nCuenta;
                    OdbcDataReader MyDataReader = cmd.ExecuteReader();
                    while (MyDataReader.Read())
                    {
                        NumeroTotalHoras = MyDataReader.GetInt32(0);
                    }
                    Close();
                    return(NumeroTotalHoras.ToString());
                }
                catch (Exception e)
                {
                    Logger.Log("Error Horas totales" + e.Message, LogType.Error);
                    Console.WriteLine("cast horas tot");
                    Close();
                    return(" ");
                }
            }
            catch (Exception e)
            {
                Logger.Log("Error Horas totales" + e.Message, LogType.Error);
                Close();
                Console.WriteLine("horas totales" + e.Message);
                return(" ");
            }
        }
Example #10
0
        public static object Value(string key)
        {
            string NewKey = TransformKey(key);

            System.Runtime.Caching.MemoryCache cache = System.Runtime.Caching.MemoryCache.Default;
            if (cache.Contains(NewKey))
            {
                return(cache[NewKey]);
            }
            return(string.Empty);
        }
            public void Put(string key, object value)
            {
                if (string.IsNullOrWhiteSpace(key))
                {
                    throw new ArgumentNullException("key", "the key is null or empty.");
                }


                if (!rootCacheKeyStored)
                {
                    StoreRootCacheKey();
                }

                string cacheKey = GetCacheKey(key);

                if (cache.Contains(key, regionName))
                {
                    cache.Set(cacheKey, new DictionaryEntry(key, value), policy, regionName);
                }
                else
                {
                    cache.Add(cacheKey, new DictionaryEntry(key, value), policy, regionName);
                }
            }
        public void RedisNotificationBus_WhenInvalidation_ShouldDisposeMonitor()
        {
            var lcache = new MemoryCache(Guid.NewGuid().ToString());
            var bus = new RedisNotificationBus("localhost:6379", new InvalidationSettings() { TargetCache = lcache, InvalidationStrategy = InvalidationStrategyType.ChangeMonitor });
            bus.Connection = this.MockOfConnection.Object;
            var monitor = new RedisChangeMonitor(bus.Notifier, "mykey");
            lcache.Add("mykey", DateTime.UtcNow, new CacheItemPolicy() { AbsoluteExpiration = DateTime.UtcNow.AddDays(1), ChangeMonitors = { monitor } });

            bus.Start();

            //act
            this.NotificationEmitter(Constants.DEFAULT_INVALIDATION_CHANNEL, "mykey");

            //assert
            Assert.False(lcache.Contains("mykey"));
            Assert.True(monitor.IsDisposed);
        }
Example #13
0
        /// <summary>
        /// Tries to get a cached object from the cache using the given cache key.
        /// </summary>
        /// <param name="cacheKey">The cache key of the object to read from the cache.</param>
        /// <param name="result">The object that was read from the cache, or null if the key
        /// could not be found in the cache.</param>
        /// <returns><c>true</c> if the item could be read from the cache, otherwise <c>false</c>.</returns>
        public virtual bool TryGetCachedObject <T>(string cacheKey, out T result)
        {
            result = default(T);
            if (!_cache.Contains(cacheKey))
            {
                return(false);
            }
            var res = _cache[cacheKey];

            if (Equals(res, nullReference))
            {
                return(true);
            }
            if (!(res is T))
            {
                return(false);
            }
            result = (T)res;
            return(true);
        }
Example #14
0
 public ISQLCacheItem GetCache(string cacheKey, out bool hasCache)
 {
     if (m_cache.ContainsKey(cacheKey))
     {
         hasCache = true;
         return(m_cache[cacheKey]);
     }
     else
     {
         if (_memoryCache.Contains(cacheKey))
         {
             hasCache = true;
             return(_memoryCache.Get(cacheKey) as ISQLCacheItem);
         }
         else
         {
             hasCache = false;
             return(null);
         }
     }
 }
Example #15
0
        /// <summary>
        /// Stores an object identified by a unique key in cache.
        /// </summary>
        /// <param name="correlationId"></param>
        /// <param name="key">Unique key identifying a data object.</param>
        /// <param name="value">The data object to store.</param>
        /// <param name="timeout">Time to live for the object in milliseconds.</param>
        public async Task <object> StoreAsync(string correlationId, string key, object value, long timeout)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            // Shortcut to remove entry from the cache
            if (value == null)
            {
                if (_standardCache.Contains(key))
                {
                    _standardCache.Remove(key);
                }
                return(null);
            }

            if (MaxSize <= _standardCache.GetCount())
            {
                lock (_lock)
                {
                    if (MaxSize <= _standardCache.GetCount())
                    {
                        _standardCache.Trim(5);
                    }
                }
            }

            timeout = timeout > 0 ? timeout : Timeout;

            _standardCache.Set(key, value, new CacheItemPolicy
            {
                SlidingExpiration = TimeSpan.FromMilliseconds(timeout)
            });

            return(await Task.FromResult(value));
        }
Example #16
0
 public static bool Exists(string key)
 {
     System.Runtime.Caching.MemoryCache cache = System.Runtime.Caching.MemoryCache.Default;
     return(cache.Contains(TransformKey(key)));
 }
Example #17
0
 private bool IsTracked(Guid id)
 {
     return(_cache.Contains(id.ToString()));
 }
Example #18
0
 public override bool Contains(string key) => _memoryCache.Contains(key);
Example #19
0
 public static bool Contains(String key)
 {
     return(_cache.Contains(key));
 }
Example #20
0
 protected override bool ExistsInternal(string key) => _cache.Contains(key);
Example #21
0
 protected override bool ExistsInternal(string key)
 {
     return(_cache.Contains(key));
 }
Example #22
0
 public bool ContainsKey(string key)
 {
     return(_cache.Contains(key));
 }
Example #23
0
 /// <summary>
 /// Determine whether the cache key exists.
 /// </summary>
 /// <typeparam name="T">item type</typeparam>
 /// <param name="key">item key</param>
 /// <returns>true if the key exists otherwise false</returns>
 public bool Contains <T>(NonNullable <string> key) where T : class
 {
     return(_cache.Contains(CacheKey <T>(key)));
 }