LRU 简单实现(C#版)

    internal class LRUSample
    {
        public static void Test()
        {
            LRUCache cache = new LRUCache(2);

            const string key1 = nameof(key1);
            const string key2 = nameof(key2);
            const string key3 = nameof(key3);

            const string value1 = nameof(value1);
            const string value2 = nameof(value2);
            const string value3 = nameof(value3);

            cache.Set(key1, value1);
            cache.Set(key2, value2);
            Console.WriteLine($"{key1}: {cache.Get(key1)};{key2}: {cache.Get(key2)};{key3}: {cache.Get(key3)};");

            cache.Set(key3, value3);
            Console.WriteLine($"{key1}: {cache.Get(key1)};{key2}: {cache.Get(key2)};{key3}: {cache.Get(key3)};");

            cache.Set(key1, value1);
            Console.WriteLine($"{key1}: {cache.Get(key1)};{key2}: {cache.Get(key2)};{key3}: {cache.Get(key3)};");
        }

        public class LRUCache
        {
            private class DLinkedNode
            {
                public string Key { get; set; }
                public string Value { get; set; }
                public DLinkedNode Pre { get; set; }
                public DLinkedNode Post { get; set; }
            }

            private readonly ConcurrentDictionary<string, DLinkedNode> _cache = new ConcurrentDictionary<string, DLinkedNode>();
            private int _count;
            private readonly int _capacity;
            private readonly DLinkedNode _head, _tail;

            public LRUCache(int capacity)
            {
                _count = 0;
                _capacity = capacity;

                _head = new DLinkedNode();
                _head.Pre = default;

                _tail = new DLinkedNode();
                _tail.Post = default;

                _head.Post = _tail;
                _tail.Pre = _head;
            }

            public string Get(string key)
            {
                DLinkedNode node = _cache.GetValueOrDefault(key);
                if (node == null) return default;

                MoveToHead(node); // 这里可以根据需要进行关闭,有些地方的实现,这里是没有的

                return node.Value;
            }

            public void Set(string key, string value)
            {
                DLinkedNode node = _cache.GetValueOrDefault(key);
                if (node != null)
                {
                    node.Value = value;
                    MoveToHead(node);
                    return;
                }

                DLinkedNode newNode = new DLinkedNode();
                newNode.Key = key;
                newNode.Value = value;

                _ = _cache.AddOrUpdate(key, newNode,
                (_, _) =>
                {
                    return newNode;
                });
                AddNode(newNode);

                //++_count;
                int count = Interlocked.Increment(ref _count);

                if (count > _capacity)
                {
                    DLinkedNode tail = PopTail();
                    _ = _cache.TryRemove(tail.Key, out _);
                    //--_count;
                    _ = Interlocked.Decrement(ref _count);
                }
            }

            private void AddNode(DLinkedNode node)
            {
                node.Pre = _head;
                node.Post = _head.Post;

                _head.Post.Pre = node;
                _head.Post = node;
            }

            private void RemoveNode(DLinkedNode node)
            {
                DLinkedNode pre = node.Pre;
                DLinkedNode post = node.Post;

                pre.Post = post;
                post.Pre = pre;
            }

            private void MoveToHead(DLinkedNode node)
            {
                RemoveNode(node);
                AddNode(node);
            }

            private DLinkedNode PopTail()
            {
                DLinkedNode res = _tail.Pre;
                RemoveNode(res);
                return res;
            }
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值