bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
ObjMemCache Class Reference

An in-memory cache for DapObj (DAS, DDS, ...) objects. More...

#include <ObjMemCache.h>

Collaboration diagram for ObjMemCache:
Collaboration graph

Public Member Functions

virtual void add (libdap::DapObj *obj, const std::string &key)
 Add an object to the cache and associate it with a key.
 
virtual void dump (ostream &os)
 What is in the cache.
 
virtual libdap::DapObj * get (const std::string &key)
 Get the cached pointer.
 
 ObjMemCache ()
 Initialize the DapObj cache This constructor builds a cache that will require the caller manage the purge() operations. Setting the entries_threshold property to zero disables checking the cache size in add().
 
 ObjMemCache (unsigned int entries_threshold, float purge_threshold)
 Initialize the DapObj cache to use an item count threshold.
 
virtual void purge (float fraction)
 Purge the oldest elements.
 
virtual void remove (const std::string &key)
 Remove the object associated with a key.
 
virtual unsigned int size () const
 How many items are in the cache.
 

Friends

class DDSMemCacheTest
 

Detailed Description

An in-memory cache for DapObj (DAS, DDS, ...) objects.

This cache stores pointers to DapObj objects in memory (not on disk) and thus, it is not a persistent cache. It provides no assurances regarding multi-process or thread safety. Thus, the cache should only be used by a single process - if there are several BES processes, each should have their own copy of the cache.

The cache stores pointers to objects, not objects themselves. The user of the cache must take care of copying objects that are added or accessed to/from the cache unless the lifetime of an pointer in the cache will suffice for the use at hand. For example, a cached DAS pointer can be passed to DDS::transfer_attributes(DAS *); there is no need to copy the underlying DAS. However, returning a DAS to the BES for serialization requires that a copy be made since the BES will delete the returned object.

The cache implements a LRU purge policy, where when the purge() method is called, the oldest 20% of times are removed. When an item is accessed (add() or get()), it's access time is updated, so the LRU policy is also a low-budget frequency of use policy without actually keeping count of the total number of accesses. The size (number of items, not bytes) of the cache is examined for every add() call and purge() is called if a preset threshold is exceeded. The purge level (20% by default) can be configured.

When an object is removed from the cache using remove() or purge(), it is deleted.

Note
The cache uses an unsigned long long to track the age of items in the cache. It's possible that numbers could wrap around (although that would be a very long-running process) in which case the code will think that the newest objects in the cache are the oldest and remove them. After d_entries_threshold, this will even itself out and the cache will correctly purge the oldest entries.

Definition at line 83 of file ObjMemCache.h.

Constructor & Destructor Documentation

◆ ObjMemCache() [1/2]

ObjMemCache::ObjMemCache ( )
inline

Initialize the DapObj cache This constructor builds a cache that will require the caller manage the purge() operations. Setting the entries_threshold property to zero disables checking the cache size in add().

See also
purge().

Definition at line 125 of file ObjMemCache.h.

◆ ObjMemCache() [2/2]

ObjMemCache::ObjMemCache ( unsigned int entries_threshold,
float purge_threshold )
inline

Initialize the DapObj cache to use an item count threshold.

The purge() method will be automatically run whenever the threshold value is exceeded and add() is called.

Parameters
entries_thresholdPurge the cache when this number of items are exceeded.
purge_thresholdWhen purging items, remove this fraction of the LRU items (e.g., 0.2 --> the oldest 20% items are removed)

Definition at line 137 of file ObjMemCache.h.

◆ ~ObjMemCache()

ObjMemCache::~ObjMemCache ( )
virtual

Definition at line 44 of file ObjMemCache.cc.

Member Function Documentation

◆ add()

void ObjMemCache::add ( libdap::DapObj * obj,
const std::string & key )
virtual

Add an object to the cache and associate it with a key.

Add the pointer to the cache, purging the cache of the least recently used items if the cache was initialized with a specific threshold value. If not, the caller must take care of calling the purge() method.

Parameters
objPointer to be cached; caller must copy the object if caching a copy of an object is desired
keyAssociate this key with the cached object

Definition at line 63 of file ObjMemCache.cc.

◆ dump()

virtual void ObjMemCache::dump ( ostream & os)
inlinevirtual

What is in the cache.

Parameters
osDump info to this stream

Definition at line 165 of file ObjMemCache.h.

◆ get()

DapObj * ObjMemCache::get ( const std::string & key)
virtual

Get the cached pointer.

Parameters
key
Returns

Definition at line 105 of file ObjMemCache.cc.

◆ purge()

void ObjMemCache::purge ( float fraction)
virtual

Purge the oldest elements.

Parameters
fraction(default is 0.2)

Definition at line 145 of file ObjMemCache.cc.

◆ remove()

void ObjMemCache::remove ( const std::string & key)
virtual

Remove the object associated with a key.

Parameters
key

Definition at line 85 of file ObjMemCache.cc.

◆ size()

virtual unsigned int ObjMemCache::size ( ) const
inlinevirtual

How many items are in the cache.

Returns
The number of items in the cache

Definition at line 154 of file ObjMemCache.h.

Friends And Related Symbol Documentation

◆ DDSMemCacheTest

friend class DDSMemCacheTest
friend

Definition at line 114 of file ObjMemCache.h.


The documentation for this class was generated from the following files: