Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
185 lines (114 sloc) 4.79 KB

Cache

The method Cache returns or instantiate a DALMP\Cache instance.

To use the cache features you must specify the type of cache either via DSN or by passing a DALMP\Cache instance as an argument to the method useCache.

DSN Cache format

charset://username:password@host:port/database?(type:host:port:compression)
type:Memcache, Redis, Disk.
host:The host of the Memcache, Redis server.
port:The port of the Memcache, Redis server.
compression:To use or not compression, only available for memcache.

Note

If no Cache is specified, defaults to disk cache type.

The Cache methods

The idea of using a 'cache' is to dispatch faster the results of a previous query with out need to connect again to the database and fetch the results.

There are five methods you can use within the Cache method which are:

method Normal Prepared statements
all CacheGetAll CachePGetAll
assoc CacheGetASSOC CachePGetASSOC
col CacheGetCol CachePGetCol
one CacheGetOne CachePGetOne
row CacheGetRow CachePGetRow

Note

Notice that when using "Cache" the methods are prefixed with Cache.

Constants

define('DALMP_CACHE_DIR', '/tmp/dalmp/cache/');

Defines where to store the cache when using dir cache type.

How to use

Whenever you want to use the cache, just just need to prepend the word Cache to the method you are using.

Parameters

You can have finer control over your cached queries, for this you have the following options:

Cache[P]method(TTL, <query>, key or group:X)
Cache[P]method:A normal or prepared statements method: 'all, assoc, col, one, row'
TTL:The time to live (timeout) in seconds for your query, default 3600 seconds / 1 hour if not set.
query:A normal or prepared statements query.
key or group:A unique custom key for storing the query result or the name of a caching group:X Where X is the desired name of the group.

TTL example

Cache the results for 300 seconds, 5 minutes:

Custom key example

If you specify a custom key, the query result will be stored on the cache.

On the cache engine, the (key, value) is translated to:

key:Your custom key.
value:The output of your query.

This is useful when you only want to flush certain parts of the cache, example:

Group caching, group:X

Helps to group your queries in groups, so that later you can only flush group without affecting the rest of your cache.

Note

When creating a cache group for your queries all of them must start with group:, so if you want a group called 'my_group' it should be: group:my_group.

.. seealso::

   `CacheFlush </en/latest/database/CacheFlush.html>`_ & `Cache Examples <https://github.com/nbari/DALMP/tree/master/examples/cache>`_.