Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
1  
Don't use DateTime.Now. Use DateTime.UtcNow. You don't want to introduce caching bugs when daylight savings changes kick in. – Matt Johnson Apr 1 '13 at 19:20
up vote 5 down vote accepted

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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