lingualeo/php-cache 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lingualeo/php-cache

Composer 安装命令:

composer require lingualeo/php-cache

包简介

LinguaLeo Caching Library

README 文档

README

PHP-Cache is a library designed to simplify your work with the different cache storage engines. Currently library is under heavy development so API can change in the future.

Example using Redis:

$redis = new \Redis();
$redis->connect('127.0.0.1');
$cache = new RedisCache($redis);

$cache->set('test', 'data'); //write 'data' by key 'test'
$cache->get('test'); //returns 'data'

Also atomic CAS-like operations supported (create and update). For example:

$modifier = function (&$data) {
    $data = 'value';
};
$result = $this->cache->create('test', $modifier);

Data represents the reference to information that storage already contains. You can modify or completly replace it inside the callable.

If other client will change Data before you call create you will get an AtomicViolationException. In your client code you can catch this exception and retry operation.

The main difference betweeen create and update methods is that update will do nothing in case storage doesn't contains the specified key.

Decorators

Currently library provides only one additional HotCacheDecorator which is very useful in a highloaded environment: it simply stores (and modifies) all cached data in a in-memory array. So if you call get once then all subsequent calls will be served only by hot cache without any requests to storage server.

To enable hot cache just wrap your creation of any CacheProvider object with the decorator:

$redis = new \Redis();
$redis->connect('127.0.0.1');
$cache = new HotCacheDecorator(new RedisCache($redis));

Cache key generation

Library provides simple mechanism to generate cache keys with generateCacheKey method of CacheProvider:

$key = CacheProvider::generateCacheKey('arg1', 'arg2'); //cache:arg1:arg2

You can pass any number of arguments (> 0) to this method in order to get a fresh shiny cache key. Also as the first parameter you can pass some class name and if this class will contain constant field VERSION then it's value will be mixed up with other arguments of generateCacheKey:

class Test {
    const VERSION = 2;
}
...
$key = CacheProvider::generateCacheKey('Test', 'arg1', 'arg2'); //cache:Test:arg1:arg2:2

This behavior is very useful when you work with ORM models and want to cache different versions of objects without need to drop all cache data after making some changes in a small single model.

Testing

Setup Vagrant and Virtualbox.

vagrant up
phpunit

统计信息

  • 总下载量: 12.25k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 19
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-04-17

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固