Esempio n. 1
0
    /// <summary>
    /// 添加元素
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    public void Put(K key, V value)
    {
        //先从LRU缓存中进行命中测试
        Node <K, V> node = ruCache.GetNode(key);

        if (node != null)
        {
            //lru缓存链中被命中,则添加到LFU中
            ruCache.RemoveKey(key);
            fuCache.Put(key, value);
            return;
        }

        //从LFU中进行命中测试
        node = fuCache.GetNode(key);
        if (node != null)
        {
            return;
        }

        //检测幽灵命中,已经被释放的资源重新添加
        int maxCapactity = initCapactity + initCapactity / 2;

        node = ruGhostCache.GetNode(key);
        if (node != null)
        {
            if (ruCache.Full && ruCache.Capactity < maxCapactity)
            {
                ruCache.Capactity++;
                fuCache.CapacityMax--;
            }
            ruCache.Put(key, value);
            ruGhostCache.RemoveKey(key);
            return;
        }

        node = fuGhostCache.GetNode(key);
        if (node != null)
        {
            if (fuCache.Full && fuCache.CapacityMax < maxCapactity)
            {
                fuCache.CapacityMax++;
                ruCache.Capactity--;
            }

            LFUCache <K, V> .LFUNode <K, V> fuNode = node as LFUCache <K, V> .LFUNode <K, V>;
            fuCache.Put(key, value, fuNode.RefCount);
            fuGhostCache.RemoveKey(key);
            return;
        }

        //新资源处理
        ruCache.Put(key, value);
    }
Esempio n. 2
0
    public V Get(K key)
    {
        //从LFU缓存中进行命中
        Node <K, V> node = fuCache.GetNode(key);

        if (node != null)
        {
            return(node.Value);
        }

        //从LRU缓存中进行命中
        node = ruCache.GetNode(key);
        if (node != null)
        {
            //lru缓存链中被命中,则添加到LFU中
            ruCache.RemoveKey(key);
            fuCache.Put(key, node.Value);
            return(node.Value);
        }

        return(default(V));
    }