Page Speed Optimization Libraries
1.13.35.1
|
Abstract interface for a cache. More...
#include "shared_mem_cache.h"
Public Member Functions | |
SharedMemCache (AbstractSharedMem *shm_runtime, const GoogleString &filename, Timer *timer, const Hasher *hasher, int sectors, int entries_per_sector, int blocks_per_sector, MessageHandler *handler) | |
code of ExtractPosition as well. More... | |
bool | Initialize () |
bool | Attach () |
size_t | MaxValueSize () const |
Returns the largest size of an object this cache can store. | |
GoogleString | DumpStats () |
bool | AddSectorToSnapshot (int sector_num, int64 last_checkpoint_ms, SharedMemCacheDump *dest) |
void | RestoreSnapshot (const SharedMemCacheDump &dump) |
virtual void | Get (const GoogleString &key, Callback *callback) |
virtual void | Put (const GoogleString &key, const SharedString &value) |
virtual void | Delete (const GoogleString &key) |
virtual GoogleString | Name () const |
virtual bool | IsBlocking () const |
virtual bool | IsHealthy () const |
virtual void | ShutDown () |
void | SanityCheck () |
Sanity check the cache data structures. | |
void | RegisterSnapshotFileCache (FileCache *potential_file_cache, int checkpoint_interval_sec) |
StringPiece | snapshot_path () const |
FileCache * | file_cache () const |
int64 | GetLastWriteMsForTesting (int sector_num) |
void | SetLastWriteMsForTesting (int sector_num, int64 last_checkpoint_ms) |
void | WriteOutSnapshotForTesting (int sector_num, int64 last_checkpoint_ms) |
Public Member Functions inherited from net_instaweb::CacheInterface | |
virtual void | MultiGet (MultiGetRequest *request) |
void | PutSwappingString (const GoogleString &key, GoogleString *value) |
virtual CacheInterface * | Backend () |
virtual bool | MustEncodeKeyInValueOnPut () const |
virtual void | PutWithKeyInValue (const GoogleString &key, const SharedString &key_and_value) |
Static Public Member Functions | |
static void | GlobalCleanup (AbstractSharedMem *shm_runtime, const GoogleString &filename, MessageHandler *message_handler) |
static void | ComputeDimensions (int64 size_kb, int block_entry_ratio, int sectors, int *entries_per_sector_out, int *blocks_per_sector_out, int64 *size_cap_out) |
static void | MarshalSnapshot (const SharedMemCacheDump &dump, GoogleString *out) |
Encode/Decode SharedMemCacheDump objects. | |
static void | DemarshalSnapshot (const StringPiece &marshaled, SharedMemCacheDump *out) |
static GoogleString | FormatName () |
Static Public Member Functions inherited from net_instaweb::CacheInterface | |
static const char * | KeyStateName (KeyState state) |
Static Public Attributes | |
static const int | kAssociativity = 4 |
Additional Inherited Members | |
Public Types inherited from net_instaweb::CacheInterface | |
enum | KeyState { kAvailable = 0, kNotFound = 1, kOverload = 2, kNetworkError = 3, kTimeout = 4 } |
typedef std::vector< KeyCallback > | MultiGetRequest |
Protected Member Functions inherited from net_instaweb::CacheInterface | |
void | ValidateAndReportResult (const GoogleString &key, KeyState state, Callback *callback) |
Invokes callback->ValidateCandidate() and callback->Done() as appropriate. | |
void | ReportMultiGetNotFound (MultiGetRequest *request) |
Abstract interface for a cache.
net_instaweb::SharedMemCache< kBlockSize >::SharedMemCache | ( | AbstractSharedMem * | shm_runtime, |
const GoogleString & | filename, | ||
Timer * | timer, | ||
const Hasher * | hasher, | ||
int | sectors, | ||
int | entries_per_sector, | ||
int | blocks_per_sector, | ||
MessageHandler * | handler | ||
) |
code of ExtractPosition as well.
Initializes the cache's settings, but does not actually touch the shared memory — you must call Initialize or Attach (and handle them potentially returning false) to do so. The filename parameter will be used to identify the shared memory segment, so distinct caches should use distinct values.
Callers who want checkpointing need to call RegisterSnapshotFileCache().
Precondition: hasher's raw mode must produce 13 bytes or more.
bool net_instaweb::SharedMemCache< kBlockSize >::AddSectorToSnapshot | ( | int | sector_num, |
int64 | last_checkpoint_ms, | ||
SharedMemCacheDump * | dest | ||
) |
Tries to dump the contents the specified sector to *dest, aborts early if a different thread is already working on it, and returns whether it was successful. To make sure only one thread ends up dumping the sector it compares the last_checkpoint_ms provided to the one in the sector, and only continues with the dump if they match. After a successful dump, it updates the last_checkpoint_ms in the sector to the current time.
Note: other accesses to the sector will be locked out for the duration.
bool net_instaweb::SharedMemCache< kBlockSize >::Attach | ( | ) |
Connects to already initialized state from a child process. It must be called once for every cache in every child process (that is, post-fork). Returns whether successful.
|
static |
Computes how many entries and blocks per sector a cache with total size 'size_kb' and 'sectors' should have if there are about 'block_entry_ratio' worth of blocks of data per every entry. You probably want to underestimate this ratio somewhat, since having extra entries can reduce conflicts. Also outputs size_cap, which is the limit on object size for the resulting cache.
GoogleString net_instaweb::SharedMemCache< kBlockSize >::DumpStats | ( | ) |
Returns some statistics as plaintext.
|
virtual |
Initiates a cache fetch, calling callback->ValidateCandidate() and then callback->Done(state) when done.
Note: implementations should normally invoke the callback via ValidateAndReportResult, which will combine ValidateCandidate() and Done() together properly.
Implements net_instaweb::CacheInterface.
|
static |
This should be called from the root process as it is about to exit, when no further children are expected to start.
bool net_instaweb::SharedMemCache< kBlockSize >::Initialize | ( | ) |
Sets up our shared state for use of all child processes/threads. Returns whether successful. This should be called exactly once for every cache in the root process, before forking.
If a file cache was set this restores any snapshotted sectors to shared memory.
|
inlinevirtual |
Returns true if this cache is guaranteed to call its callbacks before returning from Get and MultiGet.
Implements net_instaweb::CacheInterface.
|
inlinevirtual |
IsHealthy() is a rough estimation of whether cache is available for any operations. If it's false, caller may reasonably expect that making calls right now is useless as they will fail or have high latency. If it's true, operations should succeed, but some still may fail occasionally. The primary goal is to avoid sending commands to 'unhealthy' caches, e.g. if cache is under heavy load, we do not want to send even more requests.
Memory and file-based caches can simply return 'true'. It should be safe to call this frequently – the implementation shouldn't do much more that check a bool flag under mutex.
Implements net_instaweb::CacheInterface.
|
inlinevirtual |
The name of this CacheInterface – used for logging and debugging.
It is strongly recommended that you provide a static GoogleString FormatName(...) method for use in formatting the Name() return, and in testing, e.g. in third_party/pagespeed/system/system_caches_test.cc.
Implements net_instaweb::CacheInterface.
|
virtual |
Puts a value into the cache. The value that is passed in is not modified, but the SharedString is passed by non-const pointer because its reference count is bumped.
Implements net_instaweb::CacheInterface.
void net_instaweb::SharedMemCache< kBlockSize >::RegisterSnapshotFileCache | ( | FileCache * | potential_file_cache, |
int | checkpoint_interval_sec | ||
) |
Use the specified FileCache for loading and storing snapshots. This may be called multiple times with different FileCaches but we pick one. If the cache has the same path we were constructed with, use that. Otherwise, to handle the default shm cache case, use the cache with the path that comes first alphabetically.
void net_instaweb::SharedMemCache< kBlockSize >::RestoreSnapshot | ( | const SharedMemCacheDump & | dump | ) |
Restores entries stored in the dump into this cache. The dump may contain multiple sectors.
|
inlinevirtual |
Stops all cache activity. Further Put/Delete calls will be dropped, and MultiGet/Get will call the callback with kNotFound immediately. Note there is no Enable(); once the cache is stopped it is stopped forever. This function is intended for use during process shutdown.
Implements net_instaweb::CacheInterface.
|
static |
Note: changing this requires changing