mz / php-proxy
代理工具(例如缓存代理)
0.2
2014-07-18 23:11 UTC
Requires
- php: >=5.3.0
Requires (Dev)
- apigen/apigen: ~2
- nette/nette: ~2.1.3
- phpunit/phpunit: ~4
- squizlabs/php_codesniffer: 1.*
This package is not auto-updated.
Last update: 2024-09-24 03:30:16 UTC
README
此库提供了具有易于扩展行为的代理类。库包含可用于缓存的代理和延迟实例化的代理。
需求和依赖
如果使用--no-dev
选项安装,此库没有外部依赖。缓存行为的memcache后端需要启用php memcache
模块。
安装
php composer.phar require mz/php-proxy
基本用法
内存缓存代理
use MZ\Proxy\Shortcuts; // object proxy $object = new MyComplexClass(); $proxy = Shortcuts\wrapWithMemoryProxy($object, 5); // 5 seconds timeout (default = 0 - infinite) $proxy->foo(); // executes foo, stores result in cache $proxy->foo(); // returns cached result, real foo method is not executed $proxy->bar(); // works here too $property = $proxy->my_property // exactly as $object->my_property $proxy->my_property = 123; // exactly as $object->my_property = 123 // set cached methods $proxy->proxySetMethods(array('foo', 'bar')); $proxy->foo(); // cached $proxy->bar(); // cached $proxy->baz(); // not cached $proxy->proxySetMethods(null); // all methods cached fro this point (default) $callable = function() { // some time intensive code... }; $proxy = Shortcuts\wrapWithMemoryProxy($callable); $result = $proxy(); // executes function $result = $proxy(); // returns cached result
带有memcache的缓存代理
use MZ\Proxy\Shortcuts; $object = new MyComplexClass(); $memcache = new \Memcache(); $memcache->addServer('localhost', 11211); $proxy = Shortcuts\wrapWithMemcacheProxy( $object, 'memcache_prefix', $memcache ); $result = $proxy->foo(); // executes foo $result = $proxy->foo(); // returns cached result, real foo method is not executed
概念
扩展代理行为
待办事项
限制
代理不实现主题接口。
示例
待办事项