Feature/Caching

Caching

A rather long-standing request of mine has been a configurable caching interface for Agavi. With the storage system reorganization, implementing a stable, consistent API for using caching will be easily possible.

Since changing the storage system will break the API, this is a proposal for Agavi 2.0.

At the most basic level, caching is merely another storage engine. All caches must implement the following features:

  • read($key) (inherited from AgaviStorage?): reads data from the cache
  • write($key, $data) (inherited from AgaviStorage?): stores data to the cache
  • remove($key) (inherited from AgaviStorage?): removes data from the cache
  • clear(): clears all entries in the cache
  • add($key, $data): same as write(), but only writes data if it does not exist

The following caches have been evaluated for inclusion:

These caches may provide implementation-specific methods for their caching operations, as well as define additional required or optional configuration parameters:

  • Filesystem
    • has($key): determines whether a specified cache exists
    • append($key, $data): appends data to the cache
  • APC
    • writeConstants($key, $constants): writes an array of (global) constants to the cache
    • readConstants($key): reads an array of (global) constants from the cache
  • Memcache
    • increment($key): increments the stored value
    • decrement($key): decrements the stored value
    • replace($key, $data): same as write(), but only writes data if it does exist

Standalone Caches

As mentioned in Cleanup/Configuration#Caching, the Filesystem cache will need to be standalone (usable without a context). For this purpose, an AgaviIStandaloneCacheStorage interface will be defined.

The following caches can be provided as standalone caches:

  • Filesystem
  • APC

Note that if initialized from AgaviStorageManager, standalone caches will be provided a context and initialization parameters. In other words, it is not mandatory that the context not be used, just that the cache be usable without it.

Directory Structure

Following is the proposed directory structure:

  • src
    • storage
      • cache
        • AgaviIStandaloneCacheStorage.interface.php
        • AgaviCacheStorage.class.php
        • AgaviApcCacheStorage.class.php
        • AgaviMemcacheCacheStorage.class.php
        • AgaviFilesystemCacheStorage.class.php