Exemple #1
0
        internal JYCache(CacheConfig cacheconfig)
        {
            this.config = cacheconfig;
            if (-1 == config.ConcurrencyLevel)
            {
                config.ConcurrencyLevel = Environment.ProcessorCount * 2;
            }
            Contract.Assert(config.ConcurrencyLevel > 0);
            if (cacheconfig.WeakKey && config.WeakValue)
            {
                kvState   = 1;
                dicWeakKV = new ConditionalWeakTable <object, CacheEntity <WeakReference <object> > >();
            }
            else if (config.WeakKey)
            {
                kvState    = 2;
                dicWeakKey = new ConditionalWeakTable <object, CacheEntity <TValue> >();
            }
            else if (config.WeakValue)
            {
                kvState      = 3;
                dicWeakValue = new ConcurrentDictionary <TKey, CacheEntity <WeakReference <object> > >(config.ConcurrencyLevel, config.InitSzie);
            }
            else
            {
                kvState  = 4;
                dicCache = new ConcurrentDictionary <TKey, CacheEntity <TValue> >(config.ConcurrencyLevel, config.InitSzie);
            }
            //
            weighter       = (int)(config.MaxCacheSize * config.Weight);
            stack          = new ConcurrentStack <TKey>();
            dicKeys        = new ConcurrentDictionary <TKey, string>();
            removeEntities = new ConcurrentQueue <RemoveEntity <TKey, TValue> >();
            //策略
            switch (config.Policy)
            {
            case CachePolicy.FIFO:
                policy     = new FIFOPolicy <TValue>();
                weakpolicy = new FIFOPolicy <WeakReference <object> >();
                break;

            case CachePolicy.LFU:
                policy     = new LFUPolicy <TValue>();
                weakpolicy = new LFUPolicy <WeakReference <object> >();
                break;

            case CachePolicy.LRU:
                policy     = new LRUPolicy <TValue>();
                weakpolicy = new LRUPolicy <WeakReference <object> >();
                break;
            }
            // 启动定时器
            waitTimeOut = config.CacheTime * 1000;//转换毫秒
        }
Exemple #2
0
 public CacheCompare(IPolicyCompare <T> compare)
 {
     policy = compare;
 }