Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | 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                 // cast to void* first to avoid overhead
00082                 return cast (IPayload) cast(void*) map.get (key);
00083         }
00084 
00085         /**********************************************************************
00086 
00087                 Place an entry into the cache and associate it with the
00088                 provided key. Note that there can be only one entry for
00089                 any particular key. If two keys entries are added with
00090                 the same key, the second effectively overwrites the first.
00091 
00092                 Returns what it was given
00093 
00094         **********************************************************************/
00095 
00096         IPayload put(char[] key, IPayload entry)
00097         {
00098                 map.put (key, cast(Object) entry);
00099                 return entry;
00100         }
00101 
00102         /**********************************************************************
00103 
00104                 Remove (and return) the cache entry associated with the 
00105                 provided key. Returns null if there is no such entry.
00106 
00107         **********************************************************************/
00108 
00109         IPayload extract (char[] key)
00110         {
00111                 // cast to void* first to avoid overhead
00112                 return cast(IPayload) cast(void*) map.remove (key);
00113         }
00114 
00115         /**********************************************************************
00116 
00117                 Remove (and return) the cache entry associated with the 
00118                 provided key. Returns null if there is no such entry.
00119 
00120         **********************************************************************/
00121 
00122         IPayload extract (char[] key, ulong timeLimit)
00123         {
00124                 // cast to void* first to avoid overhead
00125                 IPayload e = cast(IPayload) cast(void*) map.get (key);
00126 
00127                 if (e)
00128                    {
00129                    // ignore if existing entry is newer
00130                    if (e.getTime > timeLimit)
00131                        return null;
00132 
00133                    // remove the entry from array, and return it
00134                    map.remove (key);
00135                    }
00136                 return e;
00137         }
00138 
00139         /**********************************************************************
00140 
00141         **********************************************************************/
00142 
00143         ICache bind (ICacheLoader loader)
00144         {
00145                 class PlainLoader : ICache
00146                 {
00147                         IMutableCache   cache;
00148                         ICacheLoader    loader;
00149 
00150                         /******************************************************
00151 
00152                         ******************************************************/
00153 
00154                         this (IMutableCache cache, ICacheLoader loader)
00155                         {
00156                                 this.cache = cache;
00157                                 this.loader = loader;
00158                         }
00159 
00160                         /******************************************************
00161 
00162                         ******************************************************/
00163 
00164                         IPayload get (char[] key)
00165                         {
00166                                 long     t;
00167                                 IPayload p = cache.get (key);
00168                                 
00169                                 if (p)
00170                                    {
00171                                    if (loader.test (p))
00172                                        return p;
00173                                    t = p.getTime ();
00174                                    }
00175                                 
00176                                 p = loader.load (key, t);
00177                                 if (p)
00178                                     cache.put (key, p);
00179                                 return p;
00180                         }
00181                 }
00182 
00183                 return new PlainLoader (this, loader);
00184         }
00185 }
00186 
00187 

Generated on Fri May 27 18:11:56 2005 for Mango by  doxygen 1.4.0