132
|
1 /*******************************************************************************
|
|
2
|
|
3 copyright: Copyright (c) 2004 Kris Bell. All rights reserved
|
|
4
|
|
5 license: BSD style: $(LICENSE)
|
|
6
|
|
7 version: July 2004: Initial release
|
|
8
|
|
9 author: Kris
|
|
10
|
|
11 *******************************************************************************/
|
|
12
|
|
13 module tango.net.cluster.CacheInvalidator;
|
|
14
|
|
15 package import tango.net.cluster.NetworkClient;
|
|
16
|
|
17 private import tango.net.cluster.NetworkMessage;
|
|
18
|
|
19 /*******************************************************************************
|
|
20
|
|
21 Utility class to invalidate specific cache entries across a
|
|
22 network. Any active CacheInvalidatee objects listening upon
|
|
23 the channel specified for this class will "wake up" whenever
|
|
24 the invalidate() method is invoked.
|
|
25
|
|
26 *******************************************************************************/
|
|
27
|
|
28 class CacheInvalidator : NetworkClient
|
|
29 {
|
|
30 private InvalidatorPayload filter;
|
|
31
|
|
32 /***********************************************************************
|
|
33
|
|
34 Construct an invalidator on the specified channel. Only
|
|
35 those CacheInvalidatee instances configured for the same
|
|
36 channel will be listening to this invalidator.
|
|
37
|
|
38 ***********************************************************************/
|
|
39
|
|
40 this (ICluster cluster, char[] channel)
|
|
41 {
|
|
42 assert (channel.length);
|
|
43 super (cluster, channel);
|
|
44
|
|
45 // this is what we'll send as an invalidation notification ...
|
|
46 this.filter = new InvalidatorPayload;
|
|
47 }
|
|
48
|
|
49 /***********************************************************************
|
|
50
|
|
51 Invalidate all network cache instances on this channel
|
|
52 using the specified key. When 'timeLimit' is specified,
|
|
53 only those cache entries with a time lesser or equal to
|
|
54 that specified will be removed. This is often useful if
|
|
55 you wish to avoid invalidating a cache (local or remote)
|
|
56 that has just been updated; simply pass the time value
|
|
57 of the 'old' IMessage as the argument.
|
|
58
|
|
59 Note that this is asynchronous! An invalidation is just
|
|
60 a request to remove the item within a short time period.
|
|
61 If you need the entry removed synchronously, you should
|
|
62 use the NetworkCache extract() method instead.
|
|
63
|
|
64 ***********************************************************************/
|
|
65
|
|
66 void invalidate (char[] key, Time timeLimit = Time.max)
|
|
67 {
|
|
68 assert (key.length);
|
|
69 filter.key (key);
|
|
70 filter.time (timeLimit);
|
|
71
|
|
72 // broadcast a message across the cluster
|
|
73 channel.broadcast (filter);
|
|
74 }
|
|
75 }
|
|
76
|
|
77
|
|
78 /*******************************************************************************
|
|
79
|
|
80 *******************************************************************************/
|
|
81
|
|
82 private class InvalidatorPayload : NetworkMessage
|
|
83 {
|
|
84 private char[] key_;
|
|
85
|
|
86 /***********************************************************************
|
|
87
|
|
88 ***********************************************************************/
|
|
89
|
|
90 char[] key ()
|
|
91 {
|
|
92 return key_;
|
|
93 }
|
|
94
|
|
95 /***********************************************************************
|
|
96
|
|
97 ***********************************************************************/
|
|
98
|
|
99 void key (char[] key)
|
|
100 {
|
|
101 assert (key.length);
|
|
102 key_ = key;
|
|
103 }
|
|
104
|
|
105 /***********************************************************************
|
|
106
|
|
107 Read our attributes, after telling our superclass to do
|
|
108 likewise. The order of this is important with respect to
|
|
109 inheritance, such that a subclass and superclass may be
|
|
110 populated in isolation where appropriate.
|
|
111
|
|
112 Note that we slice our text attribute, rather than copying
|
|
113 it. Since this class is temporal we can forego allocation
|
|
114 of memory, and just map it directly from the input buffer.
|
|
115
|
|
116 ***********************************************************************/
|
|
117
|
|
118 override void read (IReader input)
|
|
119 {
|
|
120 super.read (input);
|
|
121 input (key_);
|
|
122 }
|
|
123
|
|
124 /***********************************************************************
|
|
125
|
|
126 ***********************************************************************/
|
|
127
|
|
128 override void write (IWriter output)
|
|
129 {
|
|
130 super.write (output);
|
|
131 output (key_);
|
|
132 }
|
|
133 }
|
|
134
|