C++ Client API Reference for Oracle Coherence
14c (14.1.2.0.0)

F79659-03

coherence/net/cache/OldCache.hpp

00001 /*
00002  * Copyright (c) 2000, 2020, Oracle and/or its affiliates.
00003  *
00004  * Licensed under the Universal Permissive License v 1.0 as shown at
00005  * http://oss.oracle.com/licenses/upl.
00006  */
00007 #ifndef COH_OLD_CACHE_HPP
00008 #define COH_OLD_CACHE_HPP
00009 
00010 #include "coherence/lang.ns"
00011 
00012 #include "coherence/net/cache/CacheMap.hpp"
00013 #include "coherence/net/cache/EvictionPolicy.hpp"
00014 #include "coherence/net/cache/SimpleCacheStatistics.hpp"
00015 #include "coherence/net/cache/UnitCalculator.hpp"
00016 
00017 #include "coherence/util/Collection.hpp"
00018 #include "coherence/util/Comparator.hpp"
00019 #include "coherence/util/Filter.hpp"
00020 #include "coherence/util/InvocableMap.hpp"
00021 #include "coherence/util/Iterator.hpp"
00022 #include "coherence/util/Map.hpp"
00023 #include "coherence/util/MapKeySet.hpp"
00024 #include "coherence/util/MapListener.hpp"
00025 #include "coherence/util/MapListenerSupport.hpp"
00026 #include "coherence/util/MapValuesCollection.hpp"
00027 #include "coherence/util/Muterator.hpp"
00028 #include "coherence/util/ObservableMap.hpp"
00029 #include "coherence/util/SafeHashMap.hpp"
00030 #include "coherence/util/Set.hpp"
00031 #include "coherence/util/ValueExtractor.hpp"
00032 
00033 
00034 
00035 COH_OPEN_NAMESPACE3(coherence,net,cache)
00036 
00037 using coherence::util::Collection;
00038 using coherence::util::Comparator;
00039 using coherence::util::Filter;
00040 using coherence::util::InvocableMap;
00041 using coherence::util::Iterator;
00042 using coherence::util::Map;
00043 using coherence::util::MapKeySet;
00044 using coherence::util::MapListener;
00045 using coherence::util::MapListenerSupport;
00046 using coherence::util::MapValuesCollection;
00047 using coherence::util::Muterator;
00048 using coherence::util::ObservableMap;
00049 using coherence::util::SafeHashMap;
00050 using coherence::util::Set;
00051 using coherence::util::ValueExtractor;
00052 
00053 
00054 /**
00055 * A generic cache manager.
00056 *
00057 * The implementation is thread safe and uses a combination of
00058 * Most Recently Used (MRU) and Most Frequently Used (MFU) caching
00059 * strategies.
00060 *
00061 * The cache is size-limited, which means that once it reaches its maximum
00062 * size ("high-water mark") it prunes itself (to its "low-water mark"). The
00063 * cache high- and low-water-marks are measured in terms of "units", and each
00064 * cached item by default uses one unit. All of the cache constructors, except
00065 * for the default constructor, require the maximum number of units to be
00066 * passed in. To change the number of units that each cache entry uses, either
00067 * set the Units property of the cache entry, or extend the Cache
00068 * implementation so that the inner Entry class calculates its own unit size.
00069 * To determine the current, high-water and low-water sizes of the cache, use
00070 * the cache object's Units, HighUnits and LowUnits properties. The HighUnits
00071 * and LowUnits properties can be changed, even after the cache is in use.
00072 * To specify the LowUnits value as a percentage when constructing the cache,
00073 * use the extended constructor taking the percentage-prune-level.
00074 *
00075 * Each cached entry expires after one hour by default. To alter this
00076 * behavior, use a constructor that takes the expiry-millis; for example, an
00077 * expiry-millis value of 10000 will expire entries after 10 seconds. The
00078 * ExpiryDelay property can also be set once the cache is in use, but it
00079 * will not affect the expiry of previously cached items.
00080 *
00081 * The cache can optionally be flushed on a periodic basis by setting
00082 * the FlushDelay property or scheduling a specific flush time by setting
00083 * the FlushTime property.
00084 *
00085 * Cache hit statistics can be obtained from the CacheHits, CacheMisses,
00086 * HitProbability, KeyHitProbability and CompositeHitProbability read-only
00087 * properties. The statistics can be reset by invoking resetHitStatistics.
00088 * The statistics are automatically reset when the cache is cleared (the clear
00089 * method).
00090 *
00091 * The OldCache implements the ObservableMap interface, meaning it provides
00092 * event notifications to any interested listener for each insert, update and
00093 * delete, including those that occur when the cache is pruned or entries
00094 * are automatically expired.
00095 *
00096 * This implementation is designed to support extension through inheritence.
00097 * When overriding the inner Entry class, the OldCache.instantiateEntry factory
00098 * method must be overridden to instantiate the correct Entry sub-class. To
00099 * override the one-unit-per-entry default behavior, extend the inner Entry
00100 * class and override the calculateUnits method.
00101 *
00102 * The C++ version of OldCache requires a call release(), in order for it to
00103 * be collected.  Not including a call to release will result in the OldCache
00104 * being leaked.
00105 *
00106 * @author nsa 2008.06.23
00107 */
00108 class COH_EXPORT OldCache
00109     : public class_spec<OldCache,
00110          extends<SafeHashMap>,
00111          implements<ObservableMap, CacheMap> >
00112     {
00113     friend class factory<OldCache>;
00114 
00115     // ----- handle definitions (needed for nested classes) -----------------
00116 
00117     public:
00118         typedef this_spec::Handle Handle;
00119         typedef this_spec::View   View;
00120         typedef this_spec::Holder Holder;
00121 
00122 
00123     // ----- constants ------------------------------------------------------
00124     public:
00125         /**
00126         * By default, the cache size (in units).
00127         */
00128         static const size32_t default_units = 1000;
00129 
00130         /**
00131         * By default, the cache entries expire after one hour.
00132         */
00133         static const int32_t default_expire  = 3600000;
00134 
00135         /**
00136         * By default, expired cache entries are flushed on a minute interval.
00137         */
00138         static const int32_t default_flush   = 60000;
00139 
00140         /**
00141         * Unit calculator configuration enum
00142         */
00143         typedef enum
00144             {
00145             /**
00146             * Specifies the default unit calculator that weighs all entries
00147             * equally as 1.
00148             */
00149             unit_calculator_fixed    = 0,
00150             /**
00151             * Specifies an external (custom) unit calculator implementation.
00152             */
00153             unit_calculator_external = 2
00154             } UnitCalculatorType;
00155 
00156         /**
00157         * By default, when the cache prunes, it reduces its entries by 25%,
00158         * meaning it retains 75% (.75) of its entries.
00159         */
00160         static float64_t getDefaultPrune();
00161 
00162 
00163     // ----- constructors ---------------------------------------------------
00164 
00165     protected:
00166         /**
00167         * Construct an instanceo of OldCache
00168         *
00169         * @param vsName           the Name of the cache to construct
00170         * @param cUnits           the number of units that the cache manager
00171         *                         will cache before pruning the cache
00172         * @param cExpiryMillis    the number of milliseconds that each cache
00173         *                         entry lives before being automatically
00174         *                         expired
00175         * @param dflPruneLevel    the percentage of the total number of units
00176         *                         that will remain after the cache manager
00177         *                         prunes the cache (i.e. this is the
00178         *                         "low water mark" value); this value is in
00179         *                         the range 0.0 to 1.0
00180         * @param cInitialBuckets  the initial number of hash buckets,
00181         *                         0 &lt; n
00182         * @param flLoadFactor     the acceptable load factor before resizing
00183         *                         occurs, 0 &lt; n, such that a load factor
00184         *                         of 1.0 causes resizing when the number of
00185         *                         entries exceeds the number of buckets
00186         * @param flGrowthRate     the rate of bucket growth when a resize
00187         *                         occurs, 0 &lt; n, such that a growth rate
00188         *                         of 1.0 will double the number of buckets:
00189         *                         bucketcount = bucketcount * (1 + growthrate)
00190         */
00191         OldCache(size32_t cUnits = default_units,
00192                 int32_t cExpiryMillis = default_expire,
00193                 float64_t dflPruneLevel = 0.75F,
00194                 size32_t cInitialBuckets = 17,
00195                 float32_t flLoadFactor = 1.0F,
00196                 float32_t flGrowthRate = 3.0F);
00197 
00198 
00199     // ----- forward declarations -------------------------------------------
00200 
00201     protected:
00202         class KeySet;
00203         class ValuesCollection;
00204 
00205 
00206     // ----- inner class: Entry ---------------------------------------------
00207 
00208     public:
00209         /**
00210         * Entry for the local cache extends SafeHashMap::Entry adding
00211         * entry statistics used for the various eviction policies
00212         */
00213         class COH_EXPORT Entry
00214             : public cloneable_spec<Entry,
00215                 extends<SafeHashMap::Entry> >
00216             {
00217             friend class factory<Entry>;
00218 
00219             // ----- constructor ----------------------------------------
00220 
00221             protected:
00222                 /**
00223                 * Create a new OldCache::Entry
00224                 *
00225                 * @param vKey     the associated key
00226                 * @param ohValue  the associated value
00227                 * @param nHash    the associated hash code
00228                 * @param hCache   reference to the cache containing this
00229                 *                 entry
00230                 */
00231                 Entry(Object::View vKey, Object::Holder ohValue,
00232                         size32_t nHash, OldCache::Handle hCache);
00233 
00234                 /**
00235                 * Copy constructor
00236                 */
00237                 Entry(const Entry& that);
00238 
00239                 /**
00240                 * Copy an Entry.
00241                 *
00242                 * @param vThat   the entry to copy
00243                 */
00244                 Entry(Entry::View vThat);
00245 
00246             // ----- LocalCache::Entry interface ------------------------
00247 
00248             public:
00249                 /**
00250                 * Determine if the cache entry has expired.
00251                 *
00252                 * @return true if the cache entry was subject to automatic
00253                 *         expiry and the current time is greater than the
00254                 *         entry's expiry time
00255                 */
00256                 virtual bool isExpired() const;
00257 
00258                 /**
00259                 * Calculate a cache priority.
00260                 *
00261                 * @return a value between 0 and 10, 0 being the highest
00262                 *         priority
00263                 */
00264                 virtual int32_t getPriority() const;
00265 
00266             protected:
00267                 /**
00268                 * Called to inform the Entry that it is no longer used.
00269                 */
00270                 virtual void discard();
00271 
00272                 /**
00273                 * Determine if this entry has already been discarded from the
00274                 * cache.
00275                 *
00276                 * @return true if this entry has been discarded
00277                 */
00278                 virtual bool isDiscarded();
00279 
00280                 /**
00281                 * Reschedule the cache entry expiration.
00282                 */
00283                 virtual void scheduleExpiry();
00284 
00285                 /**
00286                 * Called each time the entry is accessed or modified.
00287                 */
00288                 virtual void touch();
00289 
00290                 /**
00291                 * Reset the number of times that the cache entry has been
00292                 * touched. The touch count does not get reset to zero, but
00293                 * rather to a fraction of its former self; this prevents long
00294                 * lived items from gaining an unassailable advantage in the
00295                 * eviction process.
00296                 *
00297                 * @since Coherence 3.5
00298                 */
00299                 virtual void resetTouchCount();
00300 
00301             // ----- SafeHashMap::Entry interface -----------------------
00302 
00303             public:
00304                 /**
00305                 * {@inheritDoc}
00306                 */
00307                 virtual void onAdd();
00308 
00309                 /**
00310                 * {@inheritDoc}
00311                 */
00312                 virtual Object::Holder setValue(Object::Holder ohValue);
00313 
00314             // ----- helper methods -------------------------------------
00315 
00316             protected:
00317                 /**
00318                 * Calculate a cache cost for the specified object.
00319                 *
00320                 * The default implementation uses the unit calculator type
00321                 * of the containing cache.
00322                 *
00323                 * @param ohValue  the cache value to evaluate for unit cost
00324                 *
00325                 * @return an integer value 0 or greater, with a larger value
00326                 *         signifying a higher cost
00327                 */
00328                 virtual size32_t calculateUnits(Object::Holder ohValue);
00329 
00330             // ----- Object interface -----------------------------------
00331 
00332             public:
00333                 /**
00334                 * {@inheritDoc}
00335                 */
00336                 virtual TypedHandle<const String> toString() const;
00337 
00338 
00339             // ----- accessors ------------------------------------------
00340 
00341             public:
00342                 /**
00343                 * Determine when the cache entry was created.
00344                 *
00345                 * @return the date/time value, in millis, when the entry was
00346                 * created
00347                 */
00348                 virtual int64_t getCreatedMillis() const;
00349 
00350                 /**
00351                 * Determine when the cache entry will expire, if ever.
00352                 *
00353                 * @return the date/time value, in millis, when the entry will
00354                 *         (or did) expire; zero indicates no expiry
00355                 */
00356                 virtual int64_t getExpiryMillis() const;
00357 
00358                 /**
00359                 * Determine when the cache entry was last touched.
00360                 *
00361                 * @return the date/time value, in millis, when the entry was
00362                 *         most recently touched
00363                 */
00364                 virtual int64_t getLastTouchMillis() const;
00365 
00366                 /**
00367                 * Determine the number of times that the cache entry has been
00368                 * touched.
00369                 *
00370                 * @return the number of times that the cache entry has been
00371                 *         touched
00372                 */
00373                 virtual int32_t getTouchCount() const;
00374 
00375                 /**
00376                 * Determine the number of cache units used by this Entry.
00377                 *
00378                 * @return an integer value 0 or greater, with a larger value
00379                 *         signifying a higher cost; -1 implies that the Entry
00380                 *         has been discarded
00381                 */
00382                 virtual size32_t getUnits() const;
00383 
00384                 /**
00385                 * Specify when the cache entry will expire, or disable
00386                 * expiry. Note that if the cache is configured for automatic
00387                 * expiry, each subsequent update to this cache entry will
00388                 * reschedule the expiry time.
00389                 *
00390                 * @param lMillis  pass the date/time value, in millis, for
00391                 *                 when the entry will expire, or pass zero to
00392                 *                 disable automatic expiry
00393                 */
00394                 virtual void setExpiryMillis(int64_t lMillis);
00395 
00396                 /**
00397                 * Specify the number of cache units used by this Entry.
00398                 *
00399                 * @param cUnits  an integer value 0 or greater, with a larger
00400                 *                value
00401                 */
00402                 virtual void setUnits(int32_t cUnits);
00403 
00404             private:
00405                 /**
00406                 * Package Private: Obtain the next cache entry in the chain of
00407                 * cache entries for a given hash bucket.
00408                 *
00409                 * @return the next cache entry in the hash bucket
00410                 */
00411                 Entry::Handle getNext();
00412 
00413                 /**
00414                 * Package Private: Obtain the next cache entry in the chain of
00415                 * cache entries for a given hash bucket.
00416                 *
00417                 * @return the next cache entry in the hash bucket
00418                 */
00419                 Entry::View getNext() const;
00420 
00421                 /**
00422                 * Specify the next cache entry in the chain of cache entries
00423                 * for a given hash bucket.
00424                 *
00425                 * @param hEntry  the next cache entry
00426                 */
00427                 void setNext(SafeHashMap::Entry::Handle hEntry);
00428 
00429                 /**
00430                 * Determine the most significant bit of the passed integral
00431                 * value.
00432                 *
00433                 * @param n  an int
00434                 *
00435                 * @return -1 if no bits are set; otherwise, the bit position
00436                 *         <tt>p</tt> of the most significant bit such that
00437                 *         <tt>1 &lt;&lt; p</tt> is the most significant bit
00438                 *         of <tt>n</tt>
00439                 */
00440                 int32_t indexOfMSB(size32_t n) const;
00441 
00442             // ----- data members ---------------------------------------
00443 
00444             protected:
00445                 /**
00446                 * The time at which this entry was created
00447                 */
00448                 Volatile<int64_t> m_dtCreated;
00449 
00450                 /**
00451                 * The time at which this Entry was last accessed.
00452                 */
00453                 Volatile<int64_t> m_dtLastUse;
00454 
00455                 /**
00456                 * The time at which this Entry will (or did) expire.
00457                 */
00458                 Volatile<int64_t> m_dtExpiry;
00459 
00460                 /**
00461                 * The number of times that this Entry has been accessed.
00462                 */
00463                 int32_t m_cUses;
00464 
00465                 /**
00466                 * The number of units for the Entry.
00467                 */
00468                 size32_t m_cUnits;
00469 
00470                 /**
00471                 * Reference back to the original cache.
00472                 */
00473                 mutable FinalHandle<OldCache> f_hCache;
00474 
00475             friend class OldCache::KeySet;
00476             friend class OldCache;
00477             friend class OldCache::ValuesCollection;
00478             };
00479 
00480         /**
00481         * {@inheritDoc}
00482         */
00483         virtual SafeHashMap::Entry::Handle instantiateEntry(
00484                 Object::View vKey, Object::Holder ohValue,
00485                 size32_t nHash);
00486 
00487         /**
00488         * {@inheritDoc}
00489         */
00490         virtual SafeHashMap::Entry::Handle instantiateEntry(
00491                 SafeHashMap::Entry::View vThat);
00492 
00493 
00494     // ----- inner class: IteratorFilter ------------------------------------
00495 
00496     protected:
00497         /**
00498         * This iterator will filter out expired entries from the result set
00499         * and expire them.
00500         */
00501         class IteratorFilter
00502             : public class_spec<IteratorFilter,
00503                 extends<Object>,
00504                 implements<Filter> >
00505             {
00506             friend class factory<IteratorFilter>;
00507 
00508             // ----- constructors ---------------------------------------
00509 
00510             protected:
00511                 /**
00512                 * @internal
00513                 */
00514                 IteratorFilter(OldCache::Holder thCache);
00515 
00516             // ----- Filter interface -----------------------------------
00517 
00518             public:
00519                 /**
00520                 * Apply the test to the object.
00521                 *
00522                 * @param v  the object to test
00523                 *
00524                 * @return true if the test passes, false otherwise
00525                 */
00526                 virtual bool evaluate(Object::View v) const;
00527 
00528             // ----- data members ---------------------------------------
00529 
00530             protected:
00531                 FinalHolder<OldCache> f_thCache;
00532             };
00533 
00534 
00535     // ----- inner class: EntrySet -----------------------------------------
00536 
00537     protected:
00538         /**
00539         * A set of entries backed by this map.
00540         */
00541         class EntrySet
00542             : public class_spec<EntrySet,
00543                 extends<SafeHashMap::EntrySet> >
00544             {
00545             friend class factory<EntrySet>;
00546 
00547             // ----- constructor ----------------------------------------
00548 
00549             protected:
00550                 /**
00551                 * @internal
00552                 */
00553                 EntrySet(SafeHashMap::Handle hMap);
00554 
00555                 /**
00556                 * @internal
00557                 */
00558                 EntrySet(SafeHashMap::View vMap);
00559 
00560             private:
00561                 /**
00562                 * @internal
00563                 */
00564                 EntrySet(const Entry& that);
00565 
00566             // ----- Set interface --------------------------------------
00567 
00568             public:
00569                 /**
00570                 * {@inheritDoc}
00571                 */
00572                 virtual Iterator::Handle iterator() const;
00573 
00574                 /**
00575                 * {@inheritDoc}
00576                 */
00577                 virtual Muterator::Handle iterator();
00578 
00579                 /**
00580                 * {@inheritDoc}
00581                 */
00582                 virtual ObjectArray::Handle toArray(
00583                         ObjectArray::Handle hao = NULL) const;
00584             };
00585 
00586         /**
00587         * {@inheritDoc}
00588         */
00589         virtual Set::Handle instantiateEntrySet();
00590 
00591         /**
00592         * {@inheritDoc}
00593         */
00594         virtual Set::View instantiateEntrySet() const;
00595 
00596 
00597     // ----- inner class: KeySet --------------------------------------------
00598 
00599     protected:
00600         /**
00601         * A set of entries backed by this map.
00602         */
00603         class KeySet
00604             : public class_spec<KeySet,
00605                 extends<MapKeySet> >
00606             {
00607             friend class factory<KeySet>;
00608 
00609             // ----- constructor ----------------------------------------
00610 
00611             protected:
00612                 /**
00613                 * @internal
00614                 */
00615                 KeySet(SafeHashMap::Handle hMap);
00616 
00617                 /**
00618                 * @internal
00619                 */
00620                 KeySet(SafeHashMap::View vMap);
00621 
00622 
00623             // ----- Set interface --------------------------------------
00624 
00625             protected:
00626                 /**
00627                 * {@inheritDoc}
00628                 */
00629                 virtual ObjectArray::Handle toArray(
00630                         ObjectArray::Handle hoa = NULL) const;
00631             };
00632 
00633         /**
00634         * {@inheritDoc}
00635         */
00636         virtual Set::Handle instantiateKeySet();
00637 
00638         /**
00639         * {@inheritDoc}
00640         */
00641         virtual Set::View instantiateKeySet() const;
00642 
00643 
00644     // ----- inner class: ValuesCollection ----------------------------------
00645 
00646     protected:
00647         /**
00648         * A set of entries backed by this map.
00649         */
00650         class ValuesCollection
00651             : public class_spec<ValuesCollection,
00652                 extends<MapValuesCollection> >
00653             {
00654             friend class factory<ValuesCollection>;
00655 
00656             // ----- constructor ----------------------------------------
00657 
00658             protected:
00659                 /**
00660                 * @internal
00661                 */
00662                 ValuesCollection(SafeHashMap::Handle hMap);
00663 
00664                 /**
00665                 * @internal
00666                 */
00667                 ValuesCollection(SafeHashMap::View vMap);
00668 
00669             // ----- Collection interface -------------------------------
00670 
00671             public:
00672                 /**
00673                 * {@inheritDoc}
00674                 */
00675                 virtual ObjectArray::Handle toArray(
00676                         ObjectArray::Handle hoa = NULL) const;
00677             };
00678 
00679         /**
00680         * {@inheritDoc}
00681         */
00682         virtual Collection::Handle instantiateValuesCollection();
00683 
00684         /**
00685         * {@inheritDoc}
00686         */
00687         virtual Collection::View instantiateValuesCollection() const;
00688 
00689 
00690     // ----- ObservableMap interface ----------------------------------------
00691 
00692     public:
00693         /**
00694         * {@inheritDoc}
00695         */
00696         virtual void addKeyListener(MapListener::Handle hListener,
00697                 Object::View vKey, bool fLite);
00698 
00699         /**
00700         * {@inheritDoc}
00701         */
00702         virtual void removeKeyListener(MapListener::Handle hListener,
00703                 Object::View vKey);
00704 
00705         /**
00706         * {@inheritDoc}
00707         */
00708         virtual void addMapListener(MapListener::Handle hListener);
00709 
00710         /**
00711         * {@inheritDoc}
00712         */
00713         virtual void removeMapListener(MapListener::Handle hListener);
00714 
00715         /**
00716         * {@inheritDoc}
00717         */
00718         virtual void addFilterListener(MapListener::Handle hListener,
00719                 Filter::View vFilter = NULL, bool fLite = false);
00720 
00721         /**
00722         * {@inheritDoc}
00723         */
00724         virtual void removeFilterListener(MapListener::Handle hListener,
00725                 Filter::View vFilter = NULL);
00726 
00727 
00728     // ----- Map interface --------------------------------------------------
00729 
00730     public:
00731         /**
00732         * {@inheritDoc}
00733         */
00734         virtual bool isEmpty() const;
00735 
00736         /**
00737         * {@inheritDoc}
00738         */
00739         virtual size32_t size() const;
00740 
00741         /**
00742         * {@inheritDoc}
00743         */
00744         virtual bool containsKey(Object::View vKey) const;
00745 
00746         /**
00747         * {@inheritDoc}
00748         */
00749         virtual Object::Holder get(Object::View vKey) const;
00750 
00751         /**
00752         * {@inheritDoc}
00753         */
00754         using Map::get;
00755 
00756         /**
00757         * {@inheritDoc}
00758         */
00759         virtual Object::Holder put(Object::View vKey, Object::Holder ohValue);
00760 
00761         /**
00762         * {@inheritDoc}
00763         */
00764         virtual Object::Holder remove(Object::View vKey);
00765         using Map::remove;
00766 
00767         /**
00768         * {@inheritDoc}
00769         */
00770         virtual void clear();
00771 
00772 
00773     // ----- CacheMap interface ---------------------------------------------
00774     public:
00775         /**
00776         * {@inheritDoc}
00777         */
00778         virtual Object::Holder put(Object::View vKey, Object::Holder ohValue,
00779                 int64_t cMillis);
00780 
00781         /**
00782         * {@inheritDoc}
00783         */
00784         virtual Map::View getAll(Collection::View vColKeys) const;
00785 
00786 
00787     // ----- Cache management methods ---------------------------------------
00788 
00789     public:
00790         /**
00791         * Release local resources associated with the Cache.
00792         *
00793         * Releasing a cache makes it no longer usable
00794         */
00795         virtual void release();
00796 
00797         /**
00798         * Evict a specified key from the cache, as if it had expired from the
00799         * cache. If the key is not in the cache, then the method has no effect.
00800         *
00801         * @param oKey  the key to evict from the cache
00802         */
00803         virtual void evict(Object::View vKey);
00804 
00805         /**
00806         * Evict the specified keys from the cache, as if they had each expired
00807         * from the cache.
00808         *
00809         * The result of this method is defined to be semantically the same as
00810         * the following implementation:
00811         *
00812         * <tt>
00813         * for (Iterator iter = colKeys.iterator(); iter.hasNext(); )
00814         *     {
00815         *     Object oKey = iter.next();
00816         *     evict(oKey);
00817         *     }
00818         * </tt>
00819         *
00820         * @param colKeys  a collection of keys to evict from the cache
00821         */
00822         virtual void evictAll(Collection::View vColKeys);
00823 
00824         /**
00825         * Evict all entries from the cache that are no longer valid, and
00826         * potentially prune the cache size if the cache is size-limited
00827         * and its size is above the caching low water mark.
00828         */
00829         virtual void evict();
00830 
00831         /**
00832         * Returns the CacheStatistics for this cache.
00833         *
00834         * @return a CacheStatistics object
00835         */
00836         virtual CacheStatistics::View getCacheStatistics() const;
00837 
00838         /**
00839         * Returns the CacheStatistics for this cache.
00840         *
00841         * @return a CacheStatistics object
00842         */
00843         virtual CacheStatistics::Handle getCacheStatistics();
00844 
00845         /**
00846         * Determine the number of units that the cache currently stores.
00847         *
00848         * @return the current size of the cache in units
00849         */
00850         virtual size32_t getUnits() const;
00851 
00852         /**
00853         * Determine the limit of the cache size in units. The cache will prune
00854         * itself automatically once it reaches its maximum unit level. This is
00855         * often referred to as the "high water mark" of the cache.
00856         *
00857         * @return the limit of the cache size in units
00858         */
00859         virtual size32_t getHighUnits() const;
00860 
00861         /**
00862         * Update the maximum size of the cache in units. This is often referred
00863         * to as the "high water mark" of the cache.
00864         *
00865         * @param cMax  the new maximum size of the cache, in units
00866         */
00867         virtual void setHighUnits(size32_t cMax);
00868 
00869         /**
00870         * Determine the point to which the cache will shrink when it prunes.
00871         * This is often referred to as a "low water mark" of the cache.
00872         *
00873         * @return the number of units that the cache prunes to
00874         */
00875         virtual size32_t getLowUnits() const;
00876 
00877         /**
00878         * Specify the point to which the cache will shrink when it prunes.
00879         * This is often referred to as a "low water mark" of the cache.
00880         *
00881         * @param cUnits  the number of units that the cache prunes to
00882         */
00883         virtual void setLowUnits(size32_t cUnits);
00884 
00885         /**
00886         * Determine the current eviction type.
00887         *
00888         * @return one of the EVICTION_POLICY_* enumerated values
00889         */
00890         virtual EvictionPolicy::EvictionPolicyType getEvictionType() const;
00891 
00892         /**
00893         * Specify the eviction type for the cache. The type can only be
00894         * set to an external policy if an EvictionPolicy object has been
00895         * provided.
00896         *
00897         * @param nType  one of the EVICTION_POLICY_* enumerated values
00898         */
00899         virtual void setEvictionType(EvictionPolicy::EvictionPolicyType nType);
00900 
00901         /**
00902         * Determine the current external eviction policy, if any.
00903         *
00904         * @return the external eviction policy, if one has been
00905         *         provided
00906         */
00907         virtual EvictionPolicy::View getEvictionPolicy() const;
00908 
00909         /**
00910         * Determine the current external eviction policy, if any.
00911         *
00912         * @return the external eviction policy, if one has been provided
00913         */
00914         virtual EvictionPolicy::Handle getEvictionPolicy();
00915 
00916         /**
00917         * Set the external eviction policy, and change the eviction type to
00918         * eviction_policy_external. If null is passed, clear the external
00919         * eviction policy, and use the default internal policy.
00920         *
00921         * @param policy  an external eviction policy, or null to use the default
00922         *                policy
00923         */
00924         virtual void setEvictionPolicy(EvictionPolicy::Handle hPolicy);
00925 
00926         /**
00927         * Determine the current unit calculator type.
00928         *
00929         * @return one of the UNIT_CALCULATOR_* enumerated values
00930         */
00931         virtual UnitCalculatorType getUnitCalculatorType() const;
00932 
00933         /**
00934         * Specify the unit calculator type for the cache. The type can only be
00935         * set to an external unit calculator if a UnitCalculator object has been
00936         * provided.
00937         *
00938         * @param nType  one of the UNIT_CALCULATOR_* enumerated values
00939         */
00940         virtual void setUnitCalculatorType(UnitCalculatorType nType);
00941 
00942         /**
00943         * Determine the current external unit calculator, if any.
00944         *
00945         * @return the external unit calculator, if one has been provided
00946         */
00947         virtual UnitCalculator::View getUnitCalculator() const;
00948 
00949         /**
00950         * Set the external unit calculator, and change the unit calculator type to
00951         * unit_calculator_external. If null is passed, clear the external
00952         * unit calculator, and use the default unit calculator.
00953         *
00954         * @param calculator  an external unit calculator, or null to use the default
00955         *                    unit calculator
00956         */
00957         virtual void setUnitCalculator(UnitCalculator::Handle hCalculator);
00958 
00959         /**
00960         * Determine the "time to live" for each individual cache entry.
00961         *
00962         * @return the number of milliseconds that a cache entry value will live,
00963         *         or zero if cache entries are never automatically expired
00964         */
00965         virtual int32_t getExpiryDelay() const;
00966 
00967         /**
00968         * Specify the "time to live" for cache entries. This does not affect
00969         * the already-scheduled expiry of existing entries.
00970         *
00971         * @param cMillis  the number of milliseconds that cache entries will
00972         *                 live, or zero to disable automatic expiry
00973         */
00974         virtual void setExpiryDelay(int cMillis);
00975 
00976         /**
00977         * Determine the delay between cache flushes.
00978         *
00979         * @return the number of milliseconds between cache flushes, or zero which
00980         *         signifies that the cache never flushes
00981         */
00982         virtual int32_t getFlushDelay() const;
00983 
00984         /**
00985         * Specify the delay between cache flushes.
00986         *
00987         * @param cMillis  the number of milliseconds between cache flushes, or
00988         *                 zero to never flush
00989         */
00990         virtual void setFlushDelay(int32_t cMillis);
00991 
00992         /**
00993         * Determine the date/time at which the next cache flush is scheduled.
00994         * Note that the date/time may be Long.max_value, which implies that a
00995         * flush will never occur. Also note that the cache may internally adjust
00996         * the flush time to prevent a flush from occurring during certain
00997         * processing as a means to raise concurrency.
00998         *
00999         * @return the date/time value, in milliseconds, when the cache will next
01000         *         automatically flush
01001         */
01002         virtual int64_t getFlushTime() const;
01003 
01004         /**
01005         * Specify the date/time at which the next cache flush is to occur.
01006         * Note that the date/time may be Long.max_value, which implies that a
01007         * flush will never occur. A time in the past or at the present will
01008         * cause an immediate flush.
01009         *
01010         * @param lMillis  the date/time value, in milliseconds, when the cache
01011         *                 should next automatically flush
01012         */
01013         virtual void setFlushTime(int64_t lMillis);
01014 
01015         /**
01016         * Specify if the cache is allowed to hold mutable values.
01017         *
01018         * If false the cache will ensure immutability before caching any value.
01019         *
01020         * @param fAllow  true if the cache is allowed to hold mutable values
01021         */
01022         virtual void setAllowMutableValues(bool fAllow);
01023 
01024         /**
01025         * Return whether the cache is allowed to hold mutable values.
01026         *
01027         * If false the cache will ensure immutability before caching any value.
01028         *
01029         * @return  true if the cache is allowed to hold mutable values
01030         */
01031         virtual bool isAllowMutableValues() const;
01032 
01033     // ----- statistics -----------------------------------------------------
01034 
01035     public:
01036         /**
01037         * Determine the rough number of cache hits since the cache statistics
01038         * were last reset.
01039         *
01040         * @return the number of {@link #get} calls that have been served by
01041         *         existing cache entries
01042         */
01043         virtual int64_t getCacheHits() const;
01044 
01045         /**
01046         * Determine the rough number of cache misses since the cache statistics
01047         * were last reset.
01048         *
01049         * @return the number of {@link #get} calls that failed to find an
01050         *         existing cache entry because the requested key was not in the
01051         *         cache
01052         */
01053         virtual int64_t getCacheMisses() const;
01054 
01055         /**
01056         * Determine the rough probability (0 <= p <= 1) that any particular
01057         * {@link #get} invocation will be satisfied by an existing entry in
01058         * the cache, based on the statistics collected since the last reset
01059         * of the cache statistics.
01060         *
01061         * @return the cache hit probability (0 <= p <= 1)
01062         */
01063         virtual float64_t getHitProbability() const;
01064 
01065         /**
01066         * Reset the cache statistics.
01067         */
01068         virtual void resetHitStatistics();
01069 
01070 
01071     // ----- Object interface -----------------------------------------------
01072 
01073     public:
01074         /**
01075         * {@inheritDoc}
01076         */
01077         virtual TypedHandle<const String> toString() const;
01078 
01079 
01080     // ----- helper methods -------------------------------------------------
01081 
01082     public:
01083         /**
01084         * {@inheritDoc}
01085         */
01086         virtual SafeHashMap::Entry::View getEntry(Object::View vKey) const;
01087 
01088         /**
01089         * {@inheritDoc}
01090         */
01091         virtual SafeHashMap::Entry::Handle getEntry(Object::View vKey);
01092 
01093     protected:
01094         /**
01095         * Configure the eviction type and policy.
01096         *
01097         * @param nType   one of the EVICTION_POLICY_* enumerated values
01098         * @param hPolicy  an external eviction policy, or null
01099         */
01100         virtual void configureEviction(EvictionPolicy::EvictionPolicyType nType,
01101                 EvictionPolicy::Handle hPolicy);
01102 
01103         /**
01104         * Configure the unit calculator type and implementation.
01105         *
01106         * @param nType       one of the UNIT_CALCULATOR_* enumerated values
01107         * @param hCalculator  an external unit calculator, or null
01108         */
01109         virtual void configureUnitCalculator(UnitCalculatorType nType,
01110                 UnitCalculator::Handle hCalculator);
01111 
01112         /**
01113         * Locate an Entry in the hash map based on its key. If the Entry has
01114         * expired, it is removed from the hash map.
01115         *
01116         * Unlike the {@link #getEntry} method, this method does not flush the cache
01117         * (if necessary) or update cache statistics.
01118         *
01119         * @param oKey the key object to search for
01120         *
01121         * @return the Entry or null if the entry is not found in the hash map or
01122         *         has expired
01123         */
01124         virtual SafeHashMap::Entry::Handle getEntryInternal(Object::View vKey) const;
01125 
01126         /**
01127         * Check if the cache is timed out, and clear if it is.
01128         */
01129         virtual void checkFlush() const;
01130 
01131         /**
01132         * Check if the cache is timed out, and clear if it is.
01133         */
01134         virtual void checkFlush();
01135 
01136         /**
01137         * Remove an entry because it has expired.
01138         *
01139         * @param vEntry           the expired cache entry
01140         * @param fRemoveInternal  true if the cache entry still needs to be
01141         *                         removed from the cache
01142         */
01143         virtual void removeExpired(Entry::View vEntry, bool fRemoveInternal) const;
01144 
01145         /**
01146         * Remove an entry because it has expired.
01147         *
01148         * @param hEntry           the expired cache entry
01149         * @param fRemoveInternal  true if the cache entry still needs to be
01150         *                         removed from the cache
01151         */
01152         virtual void removeExpired(Entry::Handle hEntry, bool fRemoveInternal);
01153 
01154         /**
01155         * Increment the current size
01156         */
01157         virtual void incrementUnits(size32_t cDelta);
01158 
01159         /**
01160         * Decrement the current size
01161         */
01162         virtual void decrementUnits(size32_t cDelta);
01163 
01164         /**
01165         * Check if the cache is too big, and if it is prune it by discarding the
01166         * lowest priority cache entries.
01167         */
01168         virtual void checkSize();
01169 
01170         /**
01171         * Prune the cache by discarding the lowest priority cache entries.
01172         */
01173         virtual void prune();
01174 
01175         /**
01176         * Defer the next flush by scheduling it for infinity and beyond.
01177         */
01178         virtual void deferFlush();
01179 
01180         /**
01181         * Schedule the next flush.
01182         */
01183         virtual void scheduleFlush();
01184 
01185         /**
01186         * Factory pattern: instantiate a new MapEvent corresponding
01187         * to the specified parameters.
01188         *
01189         * @return a new instance of the MapEvent class (or a subclass thereof)
01190         */
01191         virtual MapEvent::Handle instantiateMapEvent(
01192                 int32_t nId, Object::View vKey, Object::Holder ohValueOld, Object::Holder ohValueNew);
01193 
01194 
01195     // ----- event dispatching ----------------------------------------------
01196 
01197     protected:
01198         /**
01199         * Accessor for the MapListenerSupport for sub-classes.
01200         *
01201         * @return the MapListenerSupport, or null if there are no listeners
01202         */
01203         virtual MapListenerSupport::Handle getMapListenerSupport();
01204 
01205         /**
01206         * Determine if the OverflowMap has any listeners at all.
01207         *
01208         * @return true iff this OverflowMap has at least one MapListener
01209         */
01210         virtual bool hasListeners();
01211 
01212         /**
01213         * Dispatch the passed event.
01214         *
01215         * @param hEvt  a CacheEvent object
01216         */
01217         virtual void dispatchEvent(MapEvent::Handle hEvt);
01218 
01219 
01220     // ----- data members ---------------------------------------------------
01221 
01222     protected:
01223         /**
01224         * The current number of units in the cache. A unit is an undefined means
01225         * of measuring cached values, and must be 0 or positive. The particular
01226         * Entry implementation being used defines the meaning of unit.
01227         */
01228         size32_t  m_cCurUnits;
01229 
01230         /**
01231         * The number of units to allow the cache to grow to before pruning.
01232         */
01233         size32_t  m_cMaxUnits;
01234 
01235         /**
01236         * The percentage of the total number of units that will remain after
01237         * the cache manager prunes the cache (i.e. this is the "low water
01238         * mark" value); this value is in the range 0.0 to 1.0.
01239         */
01240         float64_t  m_dflPruneLevel;
01241 
01242         /**
01243         * The number of units to prune the cache down to.
01244         */
01245         size32_t  m_cPruneUnits;
01246 
01247         /**
01248         * The number of milliseconds that a value will live in the cache.
01249         * Zero indicates no timeout.
01250         */
01251         size32_t m_cExpiryDelay;
01252 
01253         /**
01254         * The interval between full cache flushes, in milliseconds.
01255         */
01256         int32_t  m_cFlushDelay;
01257 
01258         /**
01259         * The time (ie System.currentTimeMillis) at which the next full cache
01260         * flush should occur.
01261         */
01262         Volatile<int64_t> m_lNextFlush;
01263 
01264         /**
01265         * The CacheStatistics object maintained by this cache.
01266         */
01267         mutable FinalHandle<SimpleCacheStatistics> f_hStats;
01268 
01269         /**
01270         * The MapListenerSupport object.
01271         */
01272         MemberHandle<MapListenerSupport> m_hListenerSupport;
01273 
01274         /**
01275         * The type of eviction policy employed by the cache; one of the
01276         * EVICTION_POLICY_* enumerated values.
01277         */
01278         EvictionPolicy::EvictionPolicyType m_nEvictionType;
01279 
01280         /**
01281         * The eviction policy; for eviction type eviction_policy_external.
01282         */
01283         MemberHandle<EvictionPolicy> m_hPolicy;
01284 
01285         /**
01286         * The type of unit calculator employed by the cache; one of the
01287         * UNIT_CALCULATOR_* enumerated values.
01288         */
01289         UnitCalculatorType m_CalculatorType;
01290 
01291         /**
01292         * The external unit calculator.
01293         */
01294         MemberView<UnitCalculator> m_vCalculator;
01295 
01296         /**
01297         * The last time that a prune was run. This value is used by the
01298         * hybrid eviction policy.
01299         * @since Coherence 3.5
01300         */
01301         int64_t m_lLastPrune;
01302 
01303         /**
01304         * For a prune cycle, this value is the average number of touches that
01305         * an entry should have. This value is used by the hybrid eviction
01306         * policy.
01307         * @since Coherence 3.5
01308         */
01309         size32_t m_cAvgTouch;
01310 
01311         /**
01312         * Allow mutable values.
01313         */
01314         bool m_fAllowMutableValues;
01315 
01316     // ----- friends --------------------------------------------------------
01317 
01318     friend class IteratorFilter;
01319     friend class KeySet;
01320     };
01321 
01322 COH_CLOSE_NAMESPACE3
01323 
01324 #endif // COH_LOCAL_CACHE_HPP
Copyright © 2000, 2025, Oracle and/or its affiliates. Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.