gdr2_Cache

gdr2_Cache is simple and yet powerful persistent cache class that supports use of transient WordPress records to store variables or objects. In the future, plan is to implement other types of storage for cached data, starting with APC. Once constructed, object sets cache method (transient is by default) and prefix used to save all the cached object names. Class has 11 public methods used to get, set and delete cached object, to manipulate default storage method and get caching statistics.

When cache class file is included, an instance of the object is created into global $gdr2_cache_core variable, set to use transient cache and to use ‘gdr2_’ as a cache names prefix.

public stats()

Echo of the caching stats.


public get_method()

Returns current instance storage method.


public set_method($method = “transient”)

  • $method: name of the storage method

Set the current instance storage method. If method is not recognized, it will remain unchanged.


public del_network($name, $method = “”)

  • $name: name of the cached object
  • $method: name of the storage method, not required, it will fall back to method set in the object instance.

Delete cached object from network cache. If method is not transient, function falls back to del_site() method.


public del_site($name, $method = “”)

  • $name: name of the cached object
  • $method: name of the storage method, not required, it will fall back to method set in the object instance.

Delete cached object from site cache.


public get_network($name, $method = “”)

  • $name: name of the cached object
  • $method: name of the storage method, not required, it will fall back to method set in the object instance.

Get cached object from network cache. If method is not transient, function falls back to get_site() method.

Function returns: false if object by specified name is not found, cached object/variable if it is found.


public get_site($name, $method = “”)

  • $name: name of the cached object
  • $method: name of the storage method, not required, it will fall back to method set in the object instance.

Get cached object from site cache.

Function returns: false if object by specified name is not found, cached object/variable if it is found.


public set_network($name, $value, $ttl = 43200, $method = “”)

  • $name: name of the cached object
  • $value: variable or string to store in cache
  • $ttl: time to live for the cached object, it will expire after specified time (in seconds)
  • $method: name of the storage method, not required, it will fall back to method set in the object instance.

Add object to network cache. If method is not transient, function falls back to get_site() method.

Function returns: true if everything is OK, false if caching failed.


public set_site($name, $value, $ttl = 43200, $method = “”)

  • $name: name of the cached object
  • $value: variable or string to store in cache
  • $ttl: time to live for the cached object, it will expire after specified time (in seconds)
  • $method: name of the storage method, not required, it will fall back to method set in the object instance.

Add object to site cache.

Function returns: true if everything is OK, false if caching failed.

Comments

Leave a Reply