jamm/memory
Composer 安装命令:
composer require jamm/memory
包简介
Key-value storage in memory. As a storage can be used: APC, Redis, Memcache, Shared memory. All storage objects have one interface, so you can switch them without changing the working code. Contains PHP Redis client.
README 文档
README
#Key-value storage in memory
As a storage can be used:
All storage objects have one interface, so you can switch them without changing the working code.
#Features:
- Tags for keys
- "Dog-pile" ("cache miss storm") and "race condition" effects are excluded
- Lock, unlock or acquire key just by one command
- Auto Unlocker - any locked key will be automatically unlocked (on exit from function or script, RAII)
- You can select keys via callback-function (Map)
- One interface for all storages - you can change storage without changing your code
- Increment() method can work with arrays, strings and numeric values
- MultiAccess class can be used for any resource, to create an access model one write multiple read
#Usage: See demo.php to get examples of code.
You can use MemoryObjects (RedisObject, CouchbaseObject, APCObject, MemcacheObject, SHMObject) as usual key-value storage: get/set/delete.
What for this library was designed is to provide additional features, such as Tags or "dog-pile" effect avoidance.
In all storages race conditions are excluded, but you can also lock keys to avoid race conditions in your algorithm:
for example, see this code:
$value = $mem->read('key');
if (some_condition()) $mem->save('key', $value . 'append');
If this code will be executed by 2 scripts simultaneously, 'append' of one script will be lost.
To avoid it, you can lock key:
if ($mem->lock_key('key', $au))
{
if (some_condition()) $mem->save('key', $value . 'append');
}
or acquire:
if ($mem->acquire_key('key', $au))
{
if (some_condition()) $mem->save('key', $value . 'append');
}
Difference between these methods is what they will do when key is locked by another process: lock_key() will just return 'false', acquire_key() will wait until key will not be unlocked (maximum time of waiting declared in code).
All 'locks' here are soft. It means keys aren't locked for write or read, but you can check, if key is 'locked' or not, and what to do with it - is decision of your script.
It was designed to avoid dead-locks and unnecessary queues of clients which waits for access the key.
Example in code:
if ($mem->lock_key('key', $au))
{
if (some_condition()) $mem->save('key', $value . 'append');
}
else
{
// key is not hard-locked actually
$mem->del('key'); // we can do this
// but we can use 'locks' to manage multi-process interactions properly and easy (see previous code examples)
}
To avoid the "Dog-pile" effect ("cache miss storm", "cache stampede"), we can use second argument of method read() - when time of expiration is near, we can try to lock key, and if key was locked - update value.
See example in demo.php.
#Requirements: You can use each storage separately, requirements are individually for storages
###PHP version: 5.3+
##For Redis:
Redis server should be installed (in debian/ubuntu: "apt-get install redis-server").
Supported version is 2.6.9 and below.
Also, phpredis (if installed) can be used as client library - just use PhpRedisObject instead of default RedisObject.
##For Couchbase:
Installed Couchbase PHP SDK
Couchbase Server 2.0
Use few nodes if you need fault-tolerance.
##For Memcache: Memcache or Memcached PHP extension should be installed.
##For APCObject: APC should be installed, and this setting should be added in php.ini (or apc.ini if you use it)
- apc.slam_defense = Off
- recommended: apc.user_ttl = 0
##For SHMObject and MultiAccess:
PHP should support shm-functions and msg-functions (--enable-shmop --enable-sysvsem --enable-sysvshm --enable-sysvmsg)
Should be used only in specific cases (e.g. mutexes), or when other extensions can not be installed.
#Storages comparison:
Redis is the best key-value storage for cache.
Use Couchbase if you need fault-tolerant and very easy scalable cluster and if you can afford it (minimal hardware requirements).
Redis, Couchbase and Memcache(d) can be used for cross-process communication. Also, data in Redis and Couchbase storages will be restored even after server reboot.
APC is a very fast and easy to use storage, but was abandoned after PHP 5.5 release with builtin opcode caching, so it will be still exist in this library, but consider to use Redis instead.
If you can't install any third-party packages, you can use Shared Memory - but your PHP should be compiled with support of shmop-functions.
Tests:
<?php
namespace Jamm\Memory\Tests;
header('Content-type: text/plain; charset=utf-8');
$testRedisObject = new TestMemoryObject(new \Jamm\Memory\RedisObject('test'));
$testRedisObject->RunTests();
$testRedisServer = new TestRedisServer();
$testRedisServer->RunTests();
$printer = new \Jamm\Tester\ResultsPrinter();
$printer->addTests($testRedisObject->getTests());
$printer->addTests($testRedisServer->getTests());
$printer->printResultsLine();
$printer->printFailedTests();
Look at the comments in demo.php for additional info. Ask, what you want to see commented.
License: MIT
jamm/memory 适用场景与选型建议
jamm/memory 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26.91k 次下载、GitHub Stars 达 133, 最近一次更新时间为 2012 年 12 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「redis」 「apc」 「caching」 「memcached」 「memory」 「race-condition」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jamm/memory 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jamm/memory 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jamm/memory 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Query caching for Laravel 5
Microservice RPC through message queues.
The CodeIgniter Redis package
Pop Cache Component for Pop PHP Framework
贝嘟分布式缓存扩展
Simple PHP caching system that uses a tmp folder in a Linux environment.
统计信息
- 总下载量: 26.91k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 133
- 点击次数: 43
- 依赖项目数: 1
- 推荐数: 4
其他信息
- 授权协议: MIT
- 更新时间: 2012-12-04