now, let's back to our classroom,
professor is talking about the architecture of cpu
,
just like the software your are creating,Redis
is the L2
cache,
and Yac
is the L1
cache for your software,
and LocalCache
is the cache manager of Redis
and Yac
.
so, if your redis server is busying, and your data does not change frequently,
LocalCache
is the right man you are finding.
If I have seen further, it is by standing on the shoulders of giants.
- a redis client with retry mechanism
- reduce redis requests by caching data in memory
- support redis connection pool
- more graceful exception handler
- disable cache on demands
- extend class
CacheService
(recommend) - use static funcitons in
CachePoolService
- use
LocalCache
class directly
- PHP 7.0+
- phpredis
- yac
composer require 'hi-man/localcache'
composer test
pool of LocalCache
, can be called statically
initialize LocalCache
get instance of LocalCache
by connection name
set cache value
get cache value
delete from cache by key
abstract class to use CachePoolService more productive
MUST implemented get redis configuration by connection identifier
MUST implemented exception handler
wrapper of CachePoolService::setCacheValue
wrapper of CachePoolService::getCacheValue
wrapper of CachePoolService::deleteByKey
provide a local cache between application and redis server
$lc = new LocalCache(
'127.0.01' /* redis host */,
'Yac prefix' /* yac prefix, empty prefix will disable yac, default value is empty, max length is 20 */,
6379 /* redis port, default value is 6379 */,
3 /* redis connection timeout, in seconds, default value is 3 */,
500000 /* redis retry interval, in microseconds, default value is 500000 */,
3 /* redis read timeout, default value is 3 */,
3 /* max retry, default value is 3 */,
0 /* redis reserved, default value is 0 */
);
the same as redis command select
, but not really issue a command request.
$lc->select(0 /* redis database index */);
the same as redis command get
, use yac cache value first, then issue a command request if cache is missing.
$lc->get(
'key' /* redis item key */,
'default value' /* default value if the key does not exists */
);
the same as redis command set
, reset yac cache value
$lc->set(
'key', /* redis item key */
'value', /* value to store */
3 /* ttl */
'default value' /* default value if the key does not exists */
);
the same as redis command delete
or unlink
, also delete yac cache
$lc->delete(
'key', /* redis item key */
);
$lc->unlink(
'key', /* redis item key */
);
the same as redis command expire
, also reset yac cache expire time
$lc->expire('key' /* redis item key */, 3 /* expire time in seconds */);
the same as redis command flushdb
, but flush all yac cache
$lc->clear();
the same as redis command
set yac cache timeout, set the right value for your scenario. cache invalidation is a big concept to deal with.
$lc->setLocalCacheTimeout(
'key', /* redis item key */
'value', /* value to store */
3 /* ttl */
'default value' /* default value if the key does not exists */
);
get yac cache timeout
$lc->getLocalCacheTimeout();