00001 /******************************************************************************* 00002 00003 @file CacheInvalidatee.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, July 2004 00031 @author Kris 00032 00033 00034 *******************************************************************************/ 00035 00036 module mango.cluster.CacheInvalidatee; 00037 00038 private import mango.log.Logger; 00039 00040 private import mango.cache.Payload; 00041 00042 private import mango.cache.model.ICache; 00043 00044 private import mango.cluster.model.ICluster; 00045 00046 private import mango.cluster.Client, 00047 mango.cluster.CacheInvalidator; 00048 00049 /******************************************************************************* 00050 00051 Wrapper around an ICache instance that attaches it to the network, 00052 and ensures the former complies with cache invalidation requests. 00053 Use this in conjunction with CacheInvalidator or NetworkCombo. The 00054 ICache provided should typically be synchronized against thread 00055 contention since it will potentially have entries removed from a 00056 listener thread (you won't need synchronization if you're using 00057 the concurrent hash-map ICache implementation). 00058 00059 *******************************************************************************/ 00060 00061 class CacheInvalidatee : Client, IEventListener 00062 { 00063 private IMutableCache cache; 00064 private ILogger logger; 00065 private IConsumer consumer; 00066 00067 /*********************************************************************** 00068 00069 Construct a CacheInvalidatee upon the given cache, using 00070 the named channel. This channel should be a name that's 00071 common to both the receiver and the sender. 00072 00073 ***********************************************************************/ 00074 00075 this (ICluster cluster, char[] channel, IMutableCache cache) 00076 in { 00077 assert (cache); 00078 } 00079 body 00080 { 00081 super (cluster, channel); 00082 00083 this.cache = cache; 00084 this.logger = cluster.getLogger; 00085 00086 // make sure we have a logger configured 00087 if (this.logger is null) 00088 throw new ClusterException ("CacheInvalidatee requires the " 00089 "cluster to have a logger configured"); 00090 00091 // start listening for invalidation requests 00092 consumer = getCluster.createConsumer (getChannel(), IEvent.Style.Bulletin, this); 00093 } 00094 00095 /*********************************************************************** 00096 00097 Detach from the network. The CacheInvalidatee is disabled 00098 from this point forward. 00099 00100 ***********************************************************************/ 00101 00102 void cancel () 00103 { 00104 consumer.cancel (); 00105 } 00106 00107 /*********************************************************************** 00108 00109 Return the IMutableCache instance provided during construction 00110 00111 ***********************************************************************/ 00112 00113 IMutableCache getCache () 00114 { 00115 return cache; 00116 } 00117 00118 /*********************************************************************** 00119 00120 Notification callback from the listener. We remove the 00121 indicated entry from our cache and destroy the payload 00122 immediately in case it belongs on a freelist. 00123 00124 ***********************************************************************/ 00125 00126 protected void notify (IEvent event, IPayload payload) 00127 { 00128 assert (payload); 00129 00130 try { 00131 InvalidatorPayload p = cast(InvalidatorPayload) payload; 00132 00133 // remove entry from our cache 00134 if (cache.extract (p.getText, p.getTimeout)) 00135 if (logger.isEnabled (logger.Level.Trace)) 00136 logger.trace ("removed cache entry '"~p.getText~ 00137 "' on channel '"~event.getChannel.getName~"'"); 00138 } finally 00139 // place payload back on freelist? 00140 payload.destroy (); 00141 } 00142 } 00143