PHP Memory Cacher
=================
##Key-value storage in memory
As a storage may be used:
* [APC](http://pecl.php.net/package/APC)
* [Redis](http://redis.io)
* [Memcache](http://pecl.php.net/package/memcache)
* [Shared memory](http://php.net/manual/en/book.shmop.php)
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)
+ You can select keys via callback-function
+ 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](https://github.com/jamm/Memory/blob/master/demo.php) to get examples of code.
You can use MemoryObjects (RedisObject, 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 this - 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](https://github.com/jamm/Memory/blob/master/demo.php).
##Requirements:
You can use each storage separately, requirements are individually for storages
###PHP version: 5.3+ (maybe 5.2+, not checked)
###If you want to use APC:
[APC](http://pecl.php.net/package/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
###If you want to use Memcached:
[Memcache](http://pecl.php.net/package/memcache) or [Memcached](http://pecl.php.net/package/memcached) PHP extension should be installed.
Memcache is not the fastest and not secure enough storage, so use it only when it's necessary. [Read more](http://code.google.com/p/memcached/wiki/WhyNotMemcached)
###If you want to use Redis:
[Redis](http://redis.io) server should be installed (in debian/ubuntu: "apt-get install redis-server").
Supported version is 2.4 and below.
Also, [phpredis](https://github.com/nicolasff/phpredis) (if installed) can be used as client library - just use `PhpRedisObject` instead of default `RedisObject`.
###If you want to use the Unix shared memory or 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:
**APC** is a very fast and easy to use storage, and it's usually already installed on the host with PHP.
If APC can not be used for caching data, or each your php-process uses separate APC instance - use **Redis**.
Redis and Memcache can be used for cross-process communication. Also, data in Redis storage will be restored even after server reboot.
Don't want to install Redis (it's just 1 line in console :))? Use **Memcache**.
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.
##Performance comparison
+ **APC** - best performance, let speed result of APC in benchmark is 1.
+ **PhpRedis** - speed 1.23
+ **Redis** - speed 1.6
+ **Shared memory** - speed 130
+ **Memcache** - speed 192 (slowest)
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](http://en.wikipedia.org/wiki/MIT_License)
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
软件开发设计:应用软件开发、系统软件开发、移动应用开发、网站开发Node.js、C++、Java、python、web、C#等语言的项目开发与学习资料 硬件与设备:单片机、EDA、proteus、RTOS、包括计算机硬件、服务器、网络设备、存储设备、移动设备等 操作系统:LInux、Android树莓派、安卓开发、微机操作系统、网络操作系统、分布式操作系统等。此外,还有嵌入式操作系统、智能操作系统等。 网络与通信:数据传输、信号处理、网络协议、网络与通信硬件、网络安全网络与通信是一个非常广泛的领域,它涉及到计算机科学、电子工程、数学等多个学科的知识。 云计算与大数据:包括云计算平台、大数据分析、人工智能、机器学习等,云计算是一种基于互联网的计算方式,通过这种方式,共享的软硬件资源和信息可以按需提供给计算机和其他设备。
资源推荐
资源详情
资源评论
收起资源包目录
nginx+lua+php+redis实现单业务排队系统架构.zip (39个子文件)
cm
LICENSE 1KB
nginx
conf
fastcgi_params 964B
vhosts
lua_queue.com.conf 979B
nginx.conf 3KB
php_scripts
queue.php 637B
config.php 74B
index.html 5KB
redisserver
demo.php 2KB
IMemcacheDecorator.php 382B
APCObject.php 11KB
RedisObject.php 7KB
IMemoryStorage.php 3KB
MemoryObject.php 2KB
IRedisServer.php 29KB
RedisServer.php 19KB
IKeyLocker.php 192B
MemcachedDecorator.php 987B
KeyAutoUnlocker.php 569B
PhpRedisObject.php 333B
MemcachedObject.php 217B
MemcacheObject.php 12KB
README.md 6KB
Tests
TestRedisServer.php 16KB
TestMemoryObject.php 10KB
Shm
SHMObject.php 16KB
DummyMutex.php 437B
ShmMem.php 4KB
ISingleMemory.php 177B
IMutex.php 350B
SingleMemory.php 10KB
MultiAccess.php 7KB
ReadOnlyAccess.php 159B
cron_queue.php 240B
static
js
bootstrap.js 57KB
jquery.js 242KB
css
bootstrap.css 120KB
lua_scripts
content.lua 762B
ngx_lua_php_queue.png 48KB
README.md 2KB
共 39 条
- 1
资源评论
妄北y
- 粉丝: 1w+
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 前端分析-202307110078
- jsp高校学生考勤管理系统设计与实现(源代码+lw).zip
- 2023-04-06-项目笔记 - 第二百六十一阶段 - 4.4.2.259全局变量的作用域-259 -2025.09.19
- CSDN独家首发!万字长文,YOLOv5_v7_v8算法模型yaml文件史上最详细解析与教程!.mhtml
- 2023-04-06-项目笔记 - 第二百六十一阶段 - 4.4.2.259全局变量的作用域-259 -2025.09.19
- 网络设备开局配置生成器1.1.6.2正式版.rar
- 教程课件-C#教程-编程教学
- 梦幻西游道人j240919
- 小程序&预约报名&运动荟小程序(源码+截图+源码导入教程和视频).zip
- 数据库课程设计报告-学生管理系统.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功