Zebra_Cache Zebra Cache

Class: Storage_Memcached

source file: /Storage/Storage_Memcached.php

A Memcache-based implementation of the Storage_Interface.

This class provides a caching mechanism using a Memcached server as the backend.

It leverages Memcached's fast in-memory storage for high-performance caching and supports features like key expiration and atomic operations.

There are two PHP extensions for working with Memcached: the memcache extension, which is older and less commonly used, and memcached which is generally preferred for better features and compatibility.

When using Memcached as a storage engine, PHP must be compiled with either the memcache extension (PHP manual) or the memcached extension (PHP manual).

Author(s):

License:

Copyright:

  • © 2022 - 2025 Stefan Gabos

Implements interfaces:

Class methods

constructor __construct()

void __construct ( mixed $memcache )

Constructor of the class.

Initializes the Memcache client and connects to the Memcached server.

  1. // connect to a Memcached server using PHP's "Memcache" extension
  2. $memcache = new Memcache();
  3. $memcache->addServer('localhost', 11211);
  4.  
  5. // OR
  6.  
  7. // connect to a Memcached server using PHP's "Memcached" extension
  8. $memcache = new Memcached();
  9. $memcache->addServer('localhost', 11211);
  10.  
  11. // pass the $memcache instance as argument when initializing the Memcached-based storage
  12. $storage = new stefangabos\Zebra_Cache\Storage\Storage_Memcached($memcache);
  13.  
  14. // instantiate the caching library using Memcached for storage
  15. $cache = new stefangabos\Zebra_Cache\Zebra_Cache($storage);
  16.  
  17. // if a cached, non-expired value for the sought key does not exist
  18. if (!($my_data = $cache->get('my-key'))) {
  19.  
  20.     // do whatever you need to retrieve data
  21.     $my_data = 'my data';
  22.  
  23.     // cache the values for 10 minutes (10 x 60 seconds)
  24.     $cache->set('my-key', $my_data, 10 * 60);
  25.  
  26. }
  27.  
  28. // at this point $my_data will always contain data, either from cache, or fresh
Arguments
mixed $memcache An instance of either Memcache or Memcached.
top

method delete()

void delete ( string $key )

See Zebra_Cache::delete() for documentation.

Implementation of:
Storage_Interface::delete()
Deletes a cache entry.
top

method get()

void get ( string $key )

See Zebra_Cache::get() for documentation.

Implementation of:
Storage_Interface::get()
Retrieves an item from the cache.
top

method has()

mixed has ( string $key )

See Zebra_Cache::has() for full documentation.

Tags
return:  

If a cached values exists and it is not expired, the returned array will look like

  1. [
  2.     'ttl' => 'N/A',  //  memcache does not provide the remaining TTL
  3. ]
Implementation of:
Storage_Interface::has()
Checks if a cache entry exists.
top

method set()

void set ( string $key , $data , [ int $timeout = 0 ] )

See Zebra_Cache::set() for documentation.

Implementation of:
Storage_Interface::set()
Stores an item in the cache.
top