4
\$\begingroup\$

I've just implemented a cache for my server, and I'd like your thoughts about performance improvements and maybe some possible bug fixes:

public class Cache<CacheKey, CacheValue>
{
    private Dictionary<CacheKey, KeyValuePair<CacheValue, DateTime>> _map;

    /// <summary>
    /// The first element is the least recently used.
    /// </summary>
    private List<CacheKey> _lru;

    private int _maxSize;

    private ReaderWriterLock _lock;

    private int _minutes;

    public Cache(int maxNumberOfRecords, int minutes)
    {
        _maxSize = Math.Max(maxNumberOfRecords, 1);
        _lru = new List<CacheKey>(_maxSize + 1);
        _map = new Dictionary<CacheKey, KeyValuePair<CacheValue, DateTime>>();
        _lock = new ReaderWriterLock();
        _minutes = minutes;
    }

    public CacheValue find(CacheKey key)
    {
        _lock.AcquireReaderLock(-1);

        CacheValue ans;

        if (_map.ContainsKey(key) && (DateTime.Now - _map[key].Value).Minutes < _minutes)
        {
            ThreadPool.QueueUserWorkItem(o =>            //don't wait for lru to update
            {
                _lock.AcquireWriterLock(-1);
                _lru.Remove(key);
                _lru.Add(key);
                _lock.ReleaseWriterLock();
            });

            ans = _map[key].Key;
        }
        else
        {
            ans = default(CacheValue);
        }

        _lock.ReleaseReaderLock();
        return ans;
    }

    public void insert(CacheKey key, CacheValue value)
    {
        _lock.AcquireWriterLock(-1);

        if (_map.ContainsKey(key))      //if exists
        {
            _map.Remove(key);
            _map.Add(key, new KeyValuePair<CacheValue, DateTime>(value, DateTime.Now));       //update value
        }
        else
        {                               //if not
            _lru.Add(key);
            _map.Add(key, new KeyValuePair<CacheValue, DateTime>(value, DateTime.Now));       //add to cache

            if (_lru.Count > _maxSize)
            {
                _map.Remove(_lru[0]);   //and delete least recently used

                _lru.RemoveAt(0);
            }
        }

        _lock.ReleaseWriterLock();
    }
} 

I've used ThreadPool because the List operations are expensive, but I think that this hurts the LRU integrity. I'm also thinking to implement a queue with \$O(1)\$ Remove to replace the List class.

\$\endgroup\$
  • 1
    \$\begingroup\$ Don't use DateTime.Now. Use DateTime.UtcNow. You don't want to introduce caching bugs when daylight savings changes kick in. \$\endgroup\$ – Matt Johnson-Pint Apr 1 '13 at 19:20
5
\$\begingroup\$

If you are using .NET 4 or above drop your code, and use MemoryCache.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.