zoukankan      html  css  js  c++  java
  • LRUCache.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;

    namespace Shawnlee.Common.Cache
    {
    public class LRUCache<TKey,TValue>
    {
    const int DEFAULT_CAPACITY = 255;

    int _capacity;
    ReaderWriterLockSlim _locker;
    IDictionary<TKey, TValue> _dictionary;
    LinkedList<TKey> _linkedList;

    public LRUCache() : this(DEFAULT_CAPACITY) { }

    public LRUCache(int capacity)
    {
    _locker = new ReaderWriterLockSlim();
    _capacity = capacity > 0 ? capacity : DEFAULT_CAPACITY;
    _dictionary = new Dictionary<TKey, TValue>();
    _linkedList = new LinkedList<TKey>();
    }

    public void Set(TKey key, TValue value)
    {
    _locker.EnterWriteLock();
    try
    {
    _dictionary[key] = value;
    _linkedList.Remove(key);
    _linkedList.AddFirst(key);
    if (_linkedList.Count > _capacity)
    {
    _dictionary.Remove(_linkedList.Last.Value);
    _linkedList.RemoveLast();
    }
    }
    finally { _locker.ExitWriteLock(); }
    }

    public bool TryGet(TKey key, out TValue value)
    {
    _locker.EnterUpgradeableReadLock();
    try
    {
    bool b = _dictionary.TryGetValue(key, out value);
    if (b)
    {
    _locker.EnterWriteLock();
    try
    {
    _linkedList.Remove(key);
    _linkedList.AddFirst(key);
    }
    finally { _locker.ExitWriteLock(); }
    }
    return b;
    }
    catch { throw; }
    finally { _locker.ExitUpgradeableReadLock(); }
    }

    public bool ContainsKey(TKey key)
    {
    _locker.EnterReadLock();
    try
    {
    return _dictionary.ContainsKey(key);
    }
    finally { _locker.ExitReadLock(); }
    }

    public int Count
    {
    get
    {
    _locker.EnterReadLock();
    try
    {
    return _dictionary.Count;
    }
    finally { _locker.ExitReadLock(); }
    }
    }

    public int Capacity
    {
    get
    {
    _locker.EnterReadLock();
    try
    {
    return _capacity;
    }
    finally { _locker.ExitReadLock(); }
    }
    set
    {
    _locker.EnterUpgradeableReadLock();
    try
    {
    if (value > 0 && _capacity != value)
    {
    _locker.EnterWriteLock();
    try
    {
    _capacity = value;
    while (_linkedList.Count > _capacity)
    {
    _linkedList.RemoveLast();
    }
    }
    finally { _locker.ExitWriteLock(); }
    }
    }
    finally { _locker.ExitUpgradeableReadLock(); }
    }
    }

    public ICollection<TKey> Keys
    {
    get
    {
    _locker.EnterReadLock();
    try
    {
    return _dictionary.Keys;
    }
    finally { _locker.ExitReadLock(); }
    }
    }

    public ICollection<TValue> Values
    {
    get
    {
    _locker.EnterReadLock();
    try
    {
    return _dictionary.Values;
    }
    finally { _locker.ExitReadLock(); }
    }
    }
    }
    }

  • 相关阅读:
    IDEA 'Error:java: 无效的源发行版: 12' 解决方案
    E Golang语言之网络编程
    E 04 Golang语言之运算符
    ie表单提交提示下载文件
    日期初始化兼容
    IE8兼容问题总结---trim()方法
    es6变量声明和解构赋值
    js的call和apply拾遗
    prop&attr区别和用法,以多选框为例
    es6的箭头函数
  • 原文地址:https://www.cnblogs.com/mylife_001/p/6000968.html
Copyright © 2011-2022 走看看