Statistics
| Branch: | Revision:

root / trunk / hammock / src / net35 / Hammock / Caching / SimpleCache.cs @ 0eea575a

History | View | Annotate | Download (1.6 kB)

1
using System;
2
using System.Collections.Generic;
3

    
4
namespace Hammock.Caching
5
{
6
#if !SILVERLIGHT
7
    [Serializable]
8
#endif
9
    public class SimpleCache : ICache
10
    {
11
        private const string NotSupportedMessage = "This simple cache does not support expiration.";
12

    
13
        private static readonly IDictionary<string, object> _cache = new Dictionary<string, object>(0);
14

    
15
        public virtual int Count
16
        {
17
            get { return _cache.Count; }
18
        }
19

    
20
        public virtual IEnumerable<string> Keys
21
        {
22
            get { return _cache.Keys; }
23
        }
24

    
25
        #region ICache Members
26

    
27
        public virtual void Insert(string key, object value)
28
        {
29
            if (!_cache.ContainsKey(key))
30
            {
31
                _cache.Add(key, value);
32
            }
33
            else
34
            {
35
                _cache[key] = value;
36
            }
37
        }
38

    
39
        public virtual void Insert(string key, object value, DateTime absoluteExpiration)
40
        {
41
            throw new NotSupportedException(NotSupportedMessage);
42
        }
43

    
44
        public virtual void Insert(string key, object value, TimeSpan slidingExpiration)
45
        {
46
            throw new NotSupportedException(NotSupportedMessage);
47
        }
48

    
49
        public virtual T Get<T>(string key)
50
        {
51
            if (_cache.ContainsKey(key))
52
            {
53
                return (T)_cache[key];
54
            }
55
            return default(T);
56
        }
57

    
58
        public virtual void Remove(string key)
59
        {
60
            if (_cache.ContainsKey(key))
61
            {
62
                _cache.Remove(key);
63
            }
64
        }
65

    
66
        #endregion
67
    }
68
}