Zebra_Cache Zebra Cache

Class: Storage_Redis

source file: /Storage/Storage_Redis.php

A Redis-based implementation of the Storage_Interface.

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

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

When using Redis as a storage engine, PHP must be compiled with the redis extension.

Author(s):

License:

Copyright:

  • © 2022 - 2025 Stefan Gabos

Implements interfaces:

Class methods

constructor __construct()

void __construct ( Redis $redis )

Constructor of the class.

Initializes the Redis client and connects to the Redis server.

  1. // connect to a Redis server
  2. $redis = new Redis();
  3. $redis->connect('127.0.0.1', 6379);
  4.  
  5. // pass the $redis instance as argument to initialize the Redis-based storage
  6. $storage = new stefangabos\Zebra_Cache\Storage\Storage_Redis($redis);
  7.  
  8. // instantiate the caching library using Redis for storage
  9. $cache = new stefangabos\Zebra_Cache\Zebra_Cache($storage);
  10.  
  11. // if a cached, non-expired value for the sought key does not exist
  12. if (!($my_data = $cache->get('my-key'))) {
  13.  
  14.     // do whatever you need to retrieve data
  15.     $my_data = 'my data';
  16.  
  17.     // cache the values for 10 minutes (10 x 60 seconds)
  18.     $cache->set('my-key', $my_data, 10 * 60);
  19.  
  20. }
  21.  
  22. // at this point $my_data will always contain data, either from cache, or fresh
Arguments
Redis $redis An instance of Redis.
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()

void has ( string $key )

See Zebra_Cache::has() for documentation.

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