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 
00027                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00028 
00029         
00030         @version        Initial version, April 2004      
00031         @author         Kris
00032 
00033 
00034 *******************************************************************************/
00035 
00036 module mango.cache.PlainCache;
00037 
00038 public  import  mango.cache.HashMap;
00039 
00040 public  import  mango.cache.model.ICache,
00041                 mango.cache.model.IPayload;
00042 
00043 /******************************************************************************
00044 
00045         A base-class for the cache framework, using a thread-aware hash map
00046         to contain the cache entries. Cache entries must be instances of the 
00047         IPayload interface; this allows them to be moved around the network
00048         or serialized onto some external medium.
00049 
00050 ******************************************************************************/
00051 
00052 class PlainCache : IMutableCache
00053 {
00054         private HashMap map;
00055 
00056         /**********************************************************************
00057 
00058                 Construct a basic cache with the specified number of 
00059                 preallocated entries. The concurrency level indicates
00060                 approximately how many threads will content for write
00061                 access at one time.
00062 
00063         **********************************************************************/
00064 
00065         this (uint capacity = 101, uint concurrency = 16)
00066         {
00067                 map = new HashMap (capacity, 0.75, concurrency);
00068         }
00069 
00070         /**********************************************************************
00071 
00072                 Get the cache entry identified by the given key
00073 
00074         **********************************************************************/
00075 
00076         IPayload get (char[] key)
00077         {       
00078                 return cast (IPayload) map.get (key);
00079         }
00080 
00081         /**********************************************************************
00082 
00083                 Place an entry into the cache and associate it with the
00084                 provided key. Note that there can be only one entry for
00085                 any particular key. If two keys entries are added with
00086                 the same key, the second effectively overwrites the first.
00087 
00088                 Returns what it was given
00089 
00090         **********************************************************************/
00091 
00092         IPayload put(char[] key, IPayload entry)
00093         {
00094                 map.put (key, cast(Object) entry);
00095                 return entry;
00096         }
00097 
00098         /**********************************************************************
00099 
00100                 Remove (and return) the cache entry associated with the 
00101                 provided key. Returns null if there is no such entry.
00102 
00103         **********************************************************************/
00104 
00105         IPayload extract (char[] key)
00106         {
00107                 return cast(IPayload) map.remove (key);
00108         }
00109 
00110         /**********************************************************************
00111 
00112                 Remove (and return) the cache entry associated with the 
00113                 provided key. Returns null if there is no such entry.
00114 
00115         **********************************************************************/
00116 
00117         IPayload extract (char[] key, ulong timeLimit)
00118         {
00119                 IPayload e = cast(IPayload) map.get (key);
00120 
00121                 if (e)
00122                    {
00123                    // ignore if existing entry is newer
00124                    if (e.getTime > timeLimit)
00125                        return null;
00126 
00127                    // remove the entry from array, and return it
00128                    map.remove (key);
00129                    }
00130                 return e;
00131         }
00132 
00133         /**********************************************************************
00134 
00135         **********************************************************************/
00136 
00137         ICache bind (ICacheLoader loader)
00138         {
00139                 class PlainLoader : ICache
00140                 {
00141                         IMutableCache   cache;
00142                         ICacheLoader    loader;
00143 
00144                         /******************************************************
00145 
00146                         ******************************************************/
00147 
00148                         this (IMutableCache cache, ICacheLoader loader)
00149                         {
00150                                 this.cache = cache;
00151                                 this.loader = loader;
00152                         }
00153 
00154                         /******************************************************
00155 
00156                         ******************************************************/
00157 
00158                         IPayload get (char[] key)
00159                         {
00160                                 long     t;
00161                                 IPayload p = cache.get (key);
00162                                 
00163                                 if (p)
00164                                    {
00165                                    if (loader.test (p))
00166                                        return p;
00167                                    t = p.getTime ();
00168                                    }
00169                                 
00170                                 p = loader.load (key, t);
00171                                 if (p)
00172                                     cache.put (key, p);
00173                                 return p;
00174                         }
00175                 }
00176 
00177                 return new PlainLoader (this, loader);
00178         }
00179 }
00180 
00181 

Generated on Sun Nov 7 19:06:52 2004 for Mango by doxygen 1.3.6