Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

PlainCache.d

Go to the documentation of this file.
00001 /*******************************************************************************
00002 
00003         @file PlainCache.d
00004         
00005         Copyright (c) 2004 Kris Bell
00006         
00007         This software is provided 'as-is', without any express or implied
00008         warranty. In no event will the authors be held liable for damages
00009         of any kind arising from the use of this software.
00010         
00011         Permission is hereby granted to anyone to use this software for any 
00012         purpose, including commercial applications, and to alter it and/or 
00013         redistribute it freely, subject to the following restrictions:
00014         
00015         1. The origin of this software must not be misrepresented; you must 
00016            not claim that you wrote the original software. If you use this 
00017            software in a product, an acknowledgment within documentation of 
00018            said product would be appreciated but is not required.
00019 
00020         2. Altered source versions must be plainly marked as such, and must 
00021            not be misrepresented as being the original software.
00022 
00023         3. This notice may not be removed or altered from any distribution
00024            of the source.
00025 
00026         4. Derivative works are permitted, but they must carry this notice
00027            in full and credit the original source.
00028 
00029 
00030                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00031 
00032         
00033         @version        Initial version, April 2004      
00034         @author         Kris
00035 
00036 
00037 *******************************************************************************/
00038 
00039 module mango.cache.PlainCache;
00040 
00041 public  import  mango.cache.HashMap;
00042 
00043 public  import  mango.cache.model.ICache,
00044                 mango.cache.model.IPayload;
00045 
00046 /******************************************************************************
00047 
00048         A base-class for the cache framework, using a thread-aware hash map
00049         to contain the cache entries. Cache entries must be instances of the 
00050         IPayload interface; this allows them to be moved around the network
00051         or serialized onto some external medium.
00052 
00053 ******************************************************************************/
00054 
00055 class PlainCache : IMutableCache
00056 {
00057         private HashMap map;
00058 
00059         /**********************************************************************
00060 
00061                 Construct a basic cache with the specified number of 
00062                 preallocated entries. The concurrency level indicates
00063                 approximately how many threads will content for write
00064                 access at one time.
00065 
00066         **********************************************************************/
00067 
00068         this (uint capacity = 101, uint concurrency = 16)
00069         {
00070                 map = new HashMap (capacity, 0.75, concurrency);
00071         }
00072 
00073         /**********************************************************************
00074 
00075                 Get the cache entry identified by the given key
00076 
00077         **********************************************************************/
00078 
00079         IPayload get (char[] key)
00080         {       
00081                 return cast (IPayload) map.get (key);
00082         }
00083 
00084         /**********************************************************************
00085 
00086                 Place an entry into the cache and associate it with the
00087                 provided key. Note that there can be only one entry for
00088                 any particular key. If two keys entries are added with
00089                 the same key, the second effectively overwrites the first.
00090 
00091                 Returns what it was given
00092 
00093         **********************************************************************/
00094 
00095         IPayload put(char[] key, IPayload entry)
00096         {
00097                 map.put (key, cast(Object) entry);
00098                 return entry;
00099         }
00100 
00101         /**********************************************************************
00102 
00103                 Remove (and return) the cache entry associated with the 
00104                 provided key. Returns null if there is no such entry.
00105 
00106         **********************************************************************/
00107 
00108         IPayload extract (char[] key)
00109         {
00110                 return cast(IPayload) map.remove (key);
00111         }
00112 
00113         /**********************************************************************
00114 
00115                 Remove (and return) the cache entry associated with the 
00116                 provided key. Returns null if there is no such entry.
00117 
00118         **********************************************************************/
00119 
00120         IPayload extract (char[] key, ulong timeLimit)
00121         {
00122                 IPayload e = cast(IPayload) map.get (key);
00123 
00124                 if (e)
00125                    {
00126                    // ignore if existing entry is newer
00127                    if (e.getTime > timeLimit)
00128                        return null;
00129 
00130                    // remove the entry from array, and return it
00131                    map.remove (key);
00132                    }
00133                 return e;
00134         }
00135 
00136         /**********************************************************************
00137 
00138         **********************************************************************/
00139 
00140         ICache bind (ICacheLoader loader)
00141         {
00142                 class PlainLoader : ICache
00143                 {
00144                         IMutableCache   cache;
00145                         ICacheLoader    loader;
00146 
00147                         /******************************************************
00148 
00149                         ******************************************************/
00150 
00151                         this (IMutableCache cache, ICacheLoader loader)
00152                         {
00153                                 this.cache = cache;
00154                                 this.loader = loader;
00155                         }
00156 
00157                         /******************************************************
00158 
00159                         ******************************************************/
00160 
00161                         IPayload get (char[] key)
00162                         {
00163                                 long     t;
00164                                 IPayload p = cache.get (key);
00165                                 
00166                                 if (p)
00167                                    {
00168                                    if (loader.test (p))
00169                                        return p;
00170                                    t = p.getTime ();
00171                                    }
00172                                 
00173                                 p = loader.load (key, t);
00174                                 if (p)
00175                                     cache.put (key, p);
00176                                 return p;
00177                         }
00178                 }
00179 
00180                 return new PlainLoader (this, loader);
00181         }
00182 }
00183 
00184 

Generated on Tue Jan 25 21:18:22 2005 for Mango by doxygen 1.3.6