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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<?php

require_once 'dalmp.php';

$user = getenv('MYSQL_USER') ?: 'root';
$password = getenv('MYSQL_PASS') ?: '';

$DSN = "utf8://$user:$password".'@localhost/dalmp?redis:127.0.0.1:6379';

$db = new DALMP\Database($DSN);

$db->FetchMode('ASSOC');

$rs = $db->CacheGetAll(300, 'SELECT * FROM City');

echo $rs, PHP_EOL;

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<?php

require_once 'dalmp.php';

$user = getenv('MYSQL_USER') ?: 'root';
$password = getenv('MYSQL_PASS') ?: '';

$DSN = "utf8://$user:$password".'@localhost/dalmp?redis:127.0.0.1:6379';

$db = new DALMP\Database($DSN);

$db->FetchMode('ASSOC');

$rs = $db->CacheGetAll(300, 'SELECT * FROM City', 'my_custom_key');

// To flush the query
$db->CacheFlush('SELECT * FROM City', 'my_custom_key');

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<?php

require_once 'dalmp.php';

$user = getenv('MYSQL_USER') ?: 'root';
$password = getenv('MYSQL_PASS') ?: '';

$DSN = "utf8://$user:$password".'@localhost/dalmp?redis:127.0.0.1:6379';

$db = new DALMP\Database($DSN);

$db->FetchMode('ASSOC');

$rs = $db->CacheGetAll(300, 'SELECT * FROM City', 'group:B');

// To flush the group
$db->CacheFlush('group:B');

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.

Thanks Navicat for supporting Open Source projects.

Navicat



A great amount of time has been spent creating, crafting and maintaining this software, please consider donating.

Donating helps ensure continued support, development and availability.

dalmp


comments powered by Disqus