2010-04-07 Jb Evain <jbevain@novell.com>
[mcs.git] / class / Npgsql / Npgsql / Cache.cs
blob065fe5f4dd020a86c5489932e7ac8c6339ba112a
1 // created on 29/11/2007
3 // Npgsql.NpgsqlConnectionStringBuilder.cs
4 //
5 // Author:
6 // Glen Parker (glenebob@nwlink.com)
7 // Ben Sagal (bensagal@gmail.com)
8 // Tao Wang (dancefire@gmail.com)
9 //
10 // Copyright (C) 2007 The Npgsql Development Team
11 // npgsql-general@gborg.postgresql.org
12 // http://gborg.postgresql.org/project/npgsql/projdisplay.php
14 // Permission to use, copy, modify, and distribute this software and its
15 // documentation for any purpose, without fee, and without a written
16 // agreement is hereby granted, provided that the above copyright notice
17 // and this paragraph and the following two paragraphs appear in all copies.
18 //
19 // IN NO EVENT SHALL THE NPGSQL DEVELOPMENT TEAM BE LIABLE TO ANY PARTY
20 // FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
21 // INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
22 // DOCUMENTATION, EVEN IF THE NPGSQL DEVELOPMENT TEAM HAS BEEN ADVISED OF
23 // THE POSSIBILITY OF SUCH DAMAGE.
24 //
25 // THE NPGSQL DEVELOPMENT TEAM SPECIFICALLY DISCLAIMS ANY WARRANTIES,
26 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 // AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
28 // ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS
29 // TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
31 using System;
32 using System.Collections.Generic;
33 //using System.Text;
35 namespace Npgsql
37 internal class Cache<TEntity> : LinkedList<KeyValuePair<string, TEntity>>
38 where TEntity : class
40 private int _cache_size = 20;
42 /// <summary>
43 /// Set Cache Size. The default value is 20.
44 /// </summary>
45 public int CacheSize
47 get { return _cache_size; }
48 set
50 if (value < 0) { throw new ArgumentOutOfRangeException("CacheSize"); }
52 _cache_size = value;
54 if (this.Count > _cache_size)
56 lock (this)
58 while (_cache_size < this.Count)
60 RemoveLast();
67 /// <summary>
68 /// Lookup cached entity. null will returned if not match.
69 /// For both get{} and set{} apply LRU rule.
70 /// </summary>
71 /// <param name="key">key</param>
72 /// <returns></returns>
73 public TEntity this[string key]
75 get
77 lock (this)
79 for (LinkedListNode<KeyValuePair<string, TEntity>> node = this.First; node != null; node = node.Next)
81 if (node.Value.Key == key)
83 this.Remove(node);
84 this.AddFirst(node);
85 return node.Value.Value;
89 return null;
91 set
93 lock (this)
95 for (LinkedListNode<KeyValuePair<string, TEntity>> node = this.First; node != null; node = node.Next)
97 if (node.Value.Key == key)
99 this.Remove(node);
100 this.AddFirst(node);
101 return;
104 if (this.CacheSize > 0)
106 this.AddFirst(new KeyValuePair<string, TEntity>(key, value));
107 if (this.Count > this.CacheSize)
109 this.RemoveLast();
116 public Cache() : base() { }
117 public Cache(int cacheSize) : base()
119 this._cache_size = cacheSize;