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(); }
    }
    }
    }
    }

  • 相关阅读:
    Bzoj4872: [Shoi2017]分手是祝愿
    大数据应用价值研究员--数据可视化工程师
    Angular Redux
    Reactive Redux
    Testing a Redux & React web application
    [转]于Fragment和Activity之间onCreateOptionsMenu的问题
    [转]探究java IO之FileInputStream类
    深入解析FileInputStream和FileOutputStream
    [转]慎用InputStream的read()方法
    [转]Android
  • 原文地址:https://www.cnblogs.com/mylife_001/p/6000968.html
Copyright © 2011-2022 走看看