3 //=============================================================================
5 * @file Caching_Strategies_T.h
7 * @author Kirthika Parameswaran <kirthika@cs.wustl.edu>
9 //=============================================================================
11 #ifndef ACE_CACHING_STRATEGIES_H
12 #define ACE_CACHING_STRATEGIES_H
14 #include /**/ "ace/pre.h"
16 #include /**/ "ace/config-all.h"
17 #include "ace/Caching_Utility_T.h"
19 #if !defined (ACE_LACKS_PRAGMA_ONCE)
21 #endif /* ACE_LACKS_PRAGMA_ONCE */
24 #pragma warning(disable:4503)
27 // For linkers that cant grok long names.
28 #define ACE_Caching_Strategy ACS
30 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
33 * @class ACE_Caching_Strategy
35 * @brief This class is an abstract base class for a caching strategy.
37 * This class consists of all the interfaces a caching strategy should
38 * have and is used in association with the
39 * ACE_Caching_Strategy_Adaptor.
41 template <class ATTRIBUTES
, class CACHING_UTILITY
>
42 class ACE_Caching_Strategy
46 virtual ~ACE_Caching_Strategy ();
48 /// Accessor method for the timer attributes.
49 virtual ATTRIBUTES
attributes () = 0;
51 /// Get the percentage of entries to purge.
52 virtual double purge_percent () = 0;
54 /// Set the percentage of entries to purge.
55 virtual void purge_percent (double percentage
) = 0;
57 // = Strategy related Operations
59 /// This method acts as a notification about the CONTAINERs bind
61 virtual int notify_bind (int result
,
62 const ATTRIBUTES
&attr
) = 0;
64 /// This method acts as a notification about the CONTAINERs find
66 virtual int notify_find (int result
,
67 ATTRIBUTES
&attr
) = 0;
69 /// This method acts as a notification about the CONTAINERs unbind
71 virtual int notify_unbind (int result
,
72 const ATTRIBUTES
&attr
) = 0;
74 /// This method acts as a notification about the CONTAINERs trybind
76 virtual int notify_trybind (int result
,
77 ATTRIBUTES
&attr
) = 0;
79 /// This method acts as a notification about the CONTAINERs rebind
81 virtual int notify_rebind (int result
,
82 const ATTRIBUTES
&attr
) = 0;
85 virtual CACHING_UTILITY
&caching_utility () = 0;
87 /// Dumps the state of the object.
88 virtual void dump () const = 0;
91 //////////////////////////////////////////////////////////////////////////
93 #define ACE_Caching_Strategy_Adapter ACSA
96 * @class ACE_Caching_Strategy_Adapter
98 * @brief This class follows the Adaptor pattern and is used to provide
99 * External Polymorphism by deriving from ACE_Caching_Strategy.
101 * This class simply delegates all requests to the
102 * IMPLEMNETATION object within. This class should be passed in
103 * place of the the abstract base ACE_Caching_Strategy class as
104 * part of the External Polymorphism pattern.
106 template <class ATTRIBUTES
, class CACHING_UTILITY
, class IMPLEMENTATION
>
107 class ACE_Caching_Strategy_Adapter
108 : public ACE_Caching_Strategy
<ATTRIBUTES
, CACHING_UTILITY
>
112 ACE_Caching_Strategy_Adapter (IMPLEMENTATION
*implementation
= 0,
113 bool delete_implementation
= false);
116 ~ACE_Caching_Strategy_Adapter ();
118 /// Accessor method for the timer attributes.
119 ATTRIBUTES
attributes ();
121 /// Get the percentage of entries to purge.
122 double purge_percent ();
124 /// Set the percentage of entries to purge.
125 void purge_percent (double percentage
);
127 // = Strategy related Operations
129 /// This method acts as a notification about the CONTAINERs bind
131 int notify_bind (int result
,
132 const ATTRIBUTES
&attr
);
134 /// This method acts as a notification about the CONTAINERs find
136 int notify_find (int result
,
139 /// This method acts as a notification about the CONTAINERs unbind
141 int notify_unbind (int result
,
142 const ATTRIBUTES
&attr
);
144 /// This method acts as a notification about the CONTAINERs trybind
146 int notify_trybind (int result
,
149 /// This method acts as a notification about the CONTAINERs rebind
151 int notify_rebind (int result
,
152 const ATTRIBUTES
&attr
);
154 /// Accessor to the implementation.
155 IMPLEMENTATION
&implementation ();
158 CACHING_UTILITY
&caching_utility ();
160 /// Dumps the state of the object.
163 /// Declare the dynamic allocation hooks.
164 ACE_ALLOC_HOOK_DECLARE
;
167 /// Implementation class.
168 IMPLEMENTATION
*implementation_
;
170 /// Do we need to delete the implementation?
171 bool delete_implementation_
;
174 //////////////////////////////////////////////////////////////////////////
175 #define ACE_LRU_Caching_Strategy ALRU
178 * @class ACE_LRU_Caching_Strategy
180 * @brief Defines a Least Recently Used strategy which will decide on
181 * the item to be removed from the cache.
183 * This is a strategy which makes use of a virtual timer which
184 * is updated whenever an item is inserted or looked up in the
185 * container. When the need of purging entries arises, the items
186 * with the lowest timer values are removed.
187 * Explanation of the template parameter list:
188 * CONTAINER is any map with entries of type <KEY, VALUE>.
189 * The ATTRIBUTES are the deciding factor for purging of entries
190 * and should logically be included with the VALUE. Some ways of
191 * doing this are: As being a member of the VALUE or VALUE being
192 * std::pair<x, ATTRIBUTES>. The CACHING_UTILITY is the
193 * class which can be plugged in and which decides the entries
196 template <class ATTRIBUTES
, class CACHING_UTILITY
>
197 class ACE_LRU_Caching_Strategy
201 typedef ATTRIBUTES CACHING_ATTRIBUTES
;
203 // = Initialisation and termination.
206 * The <container> is the map in which the entries reside. The
207 * timer attribute is initialed to zero in this constructor. And
208 * the <purge_percent> field denotes the percentage of the entries
209 * in the cache which can be purged automagically and by default is
212 ACE_LRU_Caching_Strategy ();
214 // = Operations of the strategy.
216 /// Accessor method for the timer attributes.
217 ATTRIBUTES
attributes ();
219 /// Get the percentage of entries to purge.
220 double purge_percent ();
222 /// Set the percentage of entries to purge.
223 void purge_percent (double percentage
);
225 // = Strategy related Operations
227 /// This method acts as a notification about the CONTAINERs bind
229 int notify_bind (int result
,
230 const ATTRIBUTES
&attr
);
232 /// This method acts as a notification about the CONTAINERs find
234 int notify_find (int result
,
237 /// This method acts as a notification about the CONTAINERs unbind
239 int notify_unbind (int result
,
240 const ATTRIBUTES
&attr
);
243 /// This method acts as a notification about the CONTAINERs trybind
245 int notify_trybind (int result
,
248 /// This method acts as a notification about the CONTAINERs rebind
250 int notify_rebind (int result
,
251 const ATTRIBUTES
&attr
);
254 CACHING_UTILITY
&caching_utility ();
256 /// Dumps the state of the object.
259 /// Declare the dynamic allocation hooks.
260 ACE_ALLOC_HOOK_DECLARE
;
263 /// This element is the one which is the deciding factor for purging
267 /// The level about which the purging will happen automagically.
268 double purge_percent_
;
270 /// This is the helper class which will decide and expunge entries
272 CACHING_UTILITY caching_utility_
;
275 //////////////////////////////////////////////////////////////////////////
276 #define ACE_LFU_Caching_Strategy ALFU
279 * @class ACE_LFU_Caching_Strategy
281 * @brief Defines a Least Frequently Used strategy for which will decide on
282 * the item to be removed from the cache.
284 * A attribute is tagged to each item which increments whenever
285 * the item is bound or looked up in the cache. Thus it denotes
286 * the frequency of use. According to the value of the attribute
287 * the item is removed from the CONTAINER i.e cache.
288 * Explanation of the template parameter list:
289 * CONTAINER is any map with entries of type <KEY, VALUE>.
290 * The ATTRIBUTES are the deciding factor for purging of entries
291 * and should logically be included with the VALUE. Some ways of
292 * doing this are: As being a member of the VALUE or VALUE being
293 * std::pair<x, ATTRIBUTES>. The CACHING_UTILITY is the
294 * class which can be plugged in and which decides the entries
297 template <class ATTRIBUTES
, class CACHING_UTILITY
>
298 class ACE_LFU_Caching_Strategy
302 typedef ATTRIBUTES CACHING_ATTRIBUTES
;
304 // = Initialisation and termination methods.
307 * The <container> is the map in which the entries reside. The
308 * timer attribute is initialed to zero in this constructor. And
309 * the <purge_percent> field denotes the percentage of the entries
310 * in the cache which can be purged automagically and by default is
313 ACE_LFU_Caching_Strategy ();
315 // = Strategy methods.
317 /// Access the attributes.
318 ATTRIBUTES
attributes ();
320 /// Get the percentage of entries to purge.
321 double purge_percent ();
323 /// Set the percentage of entries to purge.
324 void purge_percent (double percentage
);
326 // = Strategy related Operations
328 /// This method acts as a notification about the CONTAINERs bind
330 int notify_bind (int result
,
331 const ATTRIBUTES
&attr
);
333 /// Lookup notification.
334 int notify_find (int result
,
337 /// This method acts as a notification about the CONTAINERs unbind
339 int notify_unbind (int result
,
340 const ATTRIBUTES
&attr
);
342 /// This method acts as a notification about the CONTAINERs trybind
344 int notify_trybind (int result
,
347 /// This method acts as a notification about the CONTAINERs rebind
349 int notify_rebind (int result
,
350 const ATTRIBUTES
&attr
);
353 CACHING_UTILITY
&caching_utility ();
355 /// Dumps the state of the object.
358 /// Declare the dynamic allocation hooks.
359 ACE_ALLOC_HOOK_DECLARE
;
362 /// The level about which the purging will happen automagically.
363 double purge_percent_
;
365 /// This is the helper class which will decide and expunge entries
367 CACHING_UTILITY caching_utility_
;
370 /////////////////////////////////////////////////////////////
371 #define ACE_FIFO_Caching_Strategy AFIFO
374 * @class ACE_FIFO_Caching_Strategy
376 * @brief The First In First Out strategy is implemented wherein each
379 * The order tag of each item is used to decide the item to be
380 * removed from the cache. The items with least order are removed.
381 * Explanation of the template parameter list:
382 * CONTAINER is any map with entries of type <KEY, VALUE>.
383 * The ATTRIBUTES are the deciding factor for purging of entries
384 * and should logically be included with the VALUE. Some ways of
385 * doing this are: As being a member of the VALUE or VALUE being
386 * std::pair<x, ATTRIBUTES>. The CACHING_UTILITY is the
387 * class which can be plugged in and which decides the entries
390 template<class ATTRIBUTES
, class CACHING_UTILITY
>
391 class ACE_FIFO_Caching_Strategy
394 typedef ATTRIBUTES CACHING_ATTRIBUTES
;
396 // = Initialisation and termination.
399 * The <container> is the map in which the entries reside. The
400 * timer attribute is initialed to zero in this constructor. And
401 * the <purge_percent> field denotes the percentage of the entries
402 * in the cache which can be purged automagically and by default is
405 ACE_FIFO_Caching_Strategy ();
407 // = Strategy methods.
410 ATTRIBUTES
attributes ();
412 /// Get the percentage of entries to purge.
413 double purge_percent ();
415 /// Set the percentage of entries to purge.
416 void purge_percent (double percentage
);
418 // = Strategy related Operations
420 /// Notification for an item getting bound into the cache.
421 int notify_bind (int result
,
422 const ATTRIBUTES
&attr
);
424 /// This method acts as a notification about the CONTAINERs find
426 int notify_find (int result
,
429 /// This method acts as a notification about the CONTAINERs unbind
431 int notify_unbind (int result
,
432 const ATTRIBUTES
&attr
);
434 /// This method acts as a notification about the CONTAINERs trybind
436 int notify_trybind (int result
,
439 /// Notification for an item getting bound again into the cache.
440 int notify_rebind (int result
,
441 const ATTRIBUTES
&attr
);
444 CACHING_UTILITY
&caching_utility ();
446 /// Dumps the state of the object.
449 /// Declare the dynamic allocation hooks.
450 ACE_ALLOC_HOOK_DECLARE
;
453 /// The order is the deciding factor for the item to be removed from
457 /// The level about which the purging will happen automagically.
458 double purge_percent_
;
460 /// This is the helper class which will decide and expunge entries
462 CACHING_UTILITY caching_utility_
;
465 //////////////////////////////////////////////////////////////////////
466 #define ACE_Null_Caching_Strategy ANULL
469 * @class ACE_Null_Caching_Strategy
471 * @brief The is a special caching strategy which doesnt have the purging
474 * No purging provided. To be used when purging might be too expensive
477 template<class ATTRIBUTES
, class CACHING_UTILITY
>
478 class ACE_Null_Caching_Strategy
482 typedef ATTRIBUTES CACHING_ATTRIBUTES
;
484 // = Strategy methods. All are NO_OP methods!!!
487 ATTRIBUTES
attributes ();
489 /// Get the percentage of entries to purge.
490 double purge_percent ();
492 /// Set the percentage of entries to purge.
493 void purge_percent (double percentage
);
495 // = Strategy related Operations
497 /// Notification for an item getting bound into the cache.
498 int notify_bind (int result
,
499 const ATTRIBUTES
&attr
);
501 /// This method acts as a notification about the CONTAINERs find
503 int notify_find (int result
,
506 /// This method acts as a notification about the CONTAINERs unbind
508 int notify_unbind (int result
,
509 const ATTRIBUTES
&attr
);
511 /// This method acts as a notification about the CONTAINERs trybind
513 int notify_trybind (int result
,
516 /// Notification for an item getting bound again into the cache.
517 int notify_rebind (int result
,
518 const ATTRIBUTES
&attr
);
521 CACHING_UTILITY
&caching_utility ();
523 /// Dumps the state of the object.
526 /// Declare the dynamic allocation hooks.
527 ACE_ALLOC_HOOK_DECLARE
;
530 /// This is the helper class which will decide and expunge entries
532 CACHING_UTILITY caching_utility_
;
535 ACE_END_VERSIONED_NAMESPACE_DECL
537 #if defined (__ACE_INLINE__)
538 #include "ace/Caching_Strategies_T.inl"
539 #endif /* __ACE_INLINE__ */
541 #include "ace/Caching_Strategies_T.cpp"
543 #include /**/ "ace/post.h"
545 #endif /* ACE_CACHING_STRATEGIES_H */