Skip to content

Phalcon cache

NOTE

All classes are prefixed with Phalcon

Cache\AbstractCache

Abstract Source on GitHub

This component offers caching capabilities for your application. Phalcon\Cache implements PSR-16.

Event layering: cache operations can emit cache:* events from two layers. This facade fires cache:before*/cache:after* around each operation, and the underlying Storage adapter (whose eventType is "cache") also fires cache:before*/cache:after* for the same operation. If an events manager is wired into both the facade and the adapter, a single call emits the event twice (once from each object). Wire the manager into one layer only; the facade is the supported source for cache-level events (it also emits the multi-key cache:*Multiple events).

Uses DateInterval · Phalcon\Cache\Adapter\AdapterInterface · Phalcon\Cache\Adapter\Redis · Phalcon\Cache\Exception\Exception · Phalcon\Cache\Exception\InvalidArgumentException · Phalcon\Events\EventsAwareInterface · Phalcon\Events\Exception · Phalcon\Events\Traits\EventsAwareTrait · Traversable

Method Summary

public __construct( AdapterInterface $adapter ) Constructor. public mixed get(string $key,mixed $default = null) Fetches a value from the cache. public AdapterInterface getAdapter() Returns the current adapter public bool set(string $key,mixed $value,DateInterval|int|null $ttl = null) Persists data in the cache, uniquely referenced by a key with an protected void checkKey( string $key ) Checks the key. If it contains invalid characters an exception is thrown protected void checkKeys( mixed $keys ) Checks the key. If it contains invalid characters an exception is thrown protected bool doClear() Wipes clean the entire cache's keys. protected bool doDelete( string $key ) Delete an item from the cache by its unique key. protected bool doDeleteMultiple( iterable $keys ) Deletes multiple cache items in a single operation. protected doGet(string $key,mixed $default = null) Fetches a value from the cache. protected iterable doGetMultiple(iterable $keys,mixed $default = null) Obtains multiple cache items by their unique keys. protected bool doHas( string $key ) Determines whether an item is present in the cache. protected bool doSet(string $key,mixed $value,DateInterval|int|null $ttl = null) Persists data in the cache, uniquely referenced by a key with an optional protected bool doSetMultiple(iterable $values,mixed $ttl = null) Persists a set of key => value pairs in the cache, with an optional TTL. protected void fire(string $eventName,mixed $keys) Trigger an event for the eventsManager. protected string getExceptionClass() Returns the exception class that will be used for exceptions thrown

Properties

protected AdapterInterface $adapter

Methods

Public · 4

__construct()

public function __construct( AdapterInterface $adapter );

Constructor.

get()

abstract public function get(
    string $key,
    mixed $default = null
): mixed;

Fetches a value from the cache.

getAdapter()

public function getAdapter(): AdapterInterface;

Returns the current adapter

set()

abstract public function set(
    string $key,
    mixed $value,
    DateInterval|int|null $ttl = null
): bool;

Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.

Protected · 12

checkKey()

protected function checkKey( string $key ): void;

Checks the key. If it contains invalid characters an exception is thrown

checkKeys()

protected function checkKeys( mixed $keys ): void;

Checks the key. If it contains invalid characters an exception is thrown

doClear()

protected function doClear(): bool;

Wipes clean the entire cache's keys.

doDelete()

protected function doDelete( string $key ): bool;

Delete an item from the cache by its unique key.

doDeleteMultiple()

protected function doDeleteMultiple( iterable $keys ): bool;

Deletes multiple cache items in a single operation.

doGet()

protected function doGet(
    string $key,
    mixed $default = null
);

Fetches a value from the cache.

doGetMultiple()

protected function doGetMultiple(
    iterable $keys,
    mixed $default = null
): iterable;

Obtains multiple cache items by their unique keys.

doHas()

protected function doHas( string $key ): bool;

Determines whether an item is present in the cache.

doSet()

protected function doSet(
    string $key,
    mixed $value,
    DateInterval|int|null $ttl = null
): bool;

Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.

doSetMultiple()

protected function doSetMultiple(
    iterable $values,
    mixed $ttl = null
): bool;

Persists a set of key => value pairs in the cache, with an optional TTL.

fire()

protected function fire(
    string $eventName,
    mixed $keys
): void;

Trigger an event for the eventsManager.

getExceptionClass()

abstract protected function getExceptionClass(): string;

Returns the exception class that will be used for exceptions thrown

Cache\AdapterFactory

Class Source on GitHub

Factory to create Cache adapters

  • Phalcon\Cache\AdapterFactory

Uses Exception · Phalcon\Cache\Adapter\AdapterInterface · Phalcon\Cache\Adapter\Apcu · Phalcon\Cache\Adapter\Libmemcached · Phalcon\Cache\Adapter\Memory · Phalcon\Cache\Adapter\Redis · Phalcon\Cache\Adapter\RedisCluster · Phalcon\Cache\Adapter\Stream · Phalcon\Cache\Adapter\Weak · Phalcon\Cache\Exception\Exception · Phalcon\Storage\SerializerFactory · Phalcon\Traits\Factory\FactoryTrait

Method Summary

Properties

protected SerializerFactory $serializerFactory

Methods

Public · 2

__construct()

public function __construct(
    SerializerFactory $serializerFactory,
    array $services = []
);

AdapterFactory constructor.

newInstance()

public function newInstance(
    string $name,
    array $options = []
): AdapterInterface;

Create a new instance of the adapter

Protected · 2

getExceptionClass()

protected function getExceptionClass(): string;

getServices()

protected function getServices(): array;

Returns the available adapters

Cache\Adapter\AdapterInterface

Interface Source on GitHub

Interface for Phalcon\Cache adapters

Uses Phalcon\Storage\Adapter\AdapterInterface

Cache\Adapter\Apcu

Class Source on GitHub

Apcu adapter

Uses Phalcon\Cache\Adapter\AdapterInterface · Phalcon\Storage\Adapter\Apcu

Properties

protected string $eventType = "cache" EventType prefix.

Cache\Adapter\Libmemcached

Class Source on GitHub

Libmemcached adapter

Uses Phalcon\Cache\Adapter\AdapterInterface · Phalcon\Storage\Adapter\Libmemcached

Properties

protected string $eventType = "cache" EventType prefix.

Cache\Adapter\Memory

Class Source on GitHub

Memory adapter

Uses Phalcon\Cache\Adapter\AdapterInterface · Phalcon\Storage\Adapter\Memory

Properties

protected string $eventType = "cache" EventType prefix.

Cache\Adapter\Redis

Class Source on GitHub

Redis adapter

Uses Phalcon\Cache\Adapter\AdapterInterface · Phalcon\Storage\Adapter\Redis

Properties

protected string $eventType = "cache" EventType prefix.

Cache\Adapter\RedisCluster

Class Source on GitHub

RedisCluster adapter

Uses Phalcon\Cache\Adapter\AdapterInterface · Phalcon\Storage\Adapter\RedisCluster

Properties

protected string $eventType = "cache" EventType prefix.

Cache\Adapter\Stream

Class Source on GitHub

Stream adapter

Uses Phalcon\Cache\Adapter\AdapterInterface · Phalcon\Storage\Adapter\Stream

Properties

protected string $eventType = "cache" EventType prefix.

Cache\Adapter\Weak

Class Source on GitHub

Weak adapter

Uses Phalcon\Cache\Adapter\AdapterInterface · Phalcon\Storage\Adapter\Weak

Properties

protected string $eventType = "cache" EventType prefix.

Cache\Cache

Class Source on GitHub

This component offers caching capabilities for your application. Phalcon\Cache implements PSR-16.

@property AdapterInterface $adapter

Uses DateInterval · Phalcon\Cache\Adapter\AdapterInterface · Phalcon\Cache\Exception\InvalidArgumentException

Method Summary

Methods

Public · 8

clear()

public function clear(): bool;

Wipes clean the entire cache's keys.

delete()

public function delete( string $key ): bool;

Delete an item from the cache by its unique key.

deleteMultiple()

public function deleteMultiple( iterable $keys ): bool;

Deletes multiple cache items in a single operation.

get()

public function get(
    string $key,
    mixed $default = null
): mixed;

Fetches a value from the cache.

getMultiple()

public function getMultiple(
    iterable $keys,
    mixed $default = null
): iterable;

Obtains multiple cache items by their unique keys.

has()

public function has( string $key ): bool;

Determines whether an item is present in the cache.

NOTE: It is recommended that has() is only to be used for cache warming type purposes and not to be used within your live applications operations for get/set, as this method is subject to a race condition where your has() will return true and immediately after, another script can remove it making the state of your app out of date.

set()

public function set(
    string $key,
    mixed $value,
    DateInterval|int|null $ttl = null
): bool;

Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.

setMultiple()

public function setMultiple(
    iterable $values,
    DateInterval|int|null $ttl = null
): bool;

Persists a set of key => value pairs in the cache, with an optional TTL.

Protected · 1

getExceptionClass()

protected function getExceptionClass(): string;

Returns the exception class that will be used for exceptions thrown

Cache\CacheFactory

Class Source on GitHub

Creates a new Cache class

@property AdapterFactory $adapterFactory;

  • Phalcon\Cache\CacheFactory

Uses Exception · Phalcon\Cache\Exception\Exception · Phalcon\Config\ConfigInterface · Phalcon\Support\Exception · Phalcon\Support\Traits\ConfigTrait

Method Summary

Properties

protected AdapterFactory $adapterFactory

Methods

Public · 3

__construct()

public function __construct( AdapterFactory $adapterFactory );

Constructor

load()

public function load( array|ConfigInterface $config ): CacheInterface;

Factory to create an instance from a Config object

newInstance()

public function newInstance(
    string $name,
    array $options = []
): CacheInterface;

Constructs a new Cache instance.

Protected · 1

getExceptionClass()

protected function getExceptionClass(): string;

Returns the exception class for the factory

Cache\CacheInterface

Interface Source on GitHub

Interface for Phalcon\Cache\Cache

Uses Phalcon\Contracts\Cache\Cache

Cache\Exception\Exception

Class Source on GitHub

Exceptions thrown in Phalcon\Cache will use this class

  • \Exception
    • Phalcon\Cache\Exception\Exception

Cache\Exception\InvalidArgumentException

Class Source on GitHub

Exceptions thrown in Phalcon\Cache for invalid arguments will use this class

  • \Exception
    • Phalcon\Cache\Exception\InvalidArgumentException