您好,欢迎访问一九零五行业门户网

基于/dev/shm的PHP缓存类

http://unbe.cn/php_file_system_based_cache_class/ 在《erlang和php间的socket通讯》中我提到做了 基于 /dev/shm的 缓存 实现的性能测试,这里分享一下测试中我封装的一个 基于 文件系统的 缓存 类,在linux上只需要把根目录指向/dev/shm,就可以变成一个
http://unbe.cn/php_file_system_based_cache_class/
在《erlang和php间的socket通讯》中我提到做了基于/dev/shm的缓存实现的性能测试,这里分享一下测试中我封装的一个基于文件系统的缓存类,在linux上只需要把根目录指向/dev/shm,就可以变成一个基于内存的缓存了,在windows上可以用普通文件系统做测试。
需要先提醒大家一点,这个缓存类只是一个原型。只是提出基于/dev/shm的缓存实现的可能性,并不是一个完整的可以在生产环境使用的缓存类。它还有很多有待完善和测试的地方,例如:数据的失效时间功能;并发情况下同一个key的数据操作;批量移除和添加并发情况下发生;等等。
使用示例:
$data_1 = array( 'u_id' => 1, 'name' => 'dada');$data_2 = array( 'u_id' => 2, 'name' => 'wawa');$cache = new file_cache(/dev/shm);$cache->set(user/1/data, $data_1); //保存数据$cache->set(user/2/data, $data_2); //保存数据$result = $cache->get(user/1/data); //获取数据$cache->remove(user/1/data); //删除数据$cache->remove_by_search(user, $data_1); //删除user节点下所有数据
由于/dev/shm是把内存模拟成文件系统,所以很容易就实现了层级式的缓存管理。这对合理利用内存空间是很有帮助的。比如一些针对用户的缓存,可以通过层级式的存储,在用户退出系统时全部移除。再比如一些同表的不同业务逻辑视图数据的缓存,在表更新后,也可以批量的移除。
root_dir = $root_dir; if (false == file_exists($this->root_dir)) { mkdir($this->root_dir, 0700, true); } } public function set ($key, $value) { $key = $this->escape_key($key); $file_name = $this->root_dir . '/' . $key; $dir = dirname($file_name); if (false == file_exists($dir)) { mkdir($dir, 0700, true); } file_put_contents($file_name, serialize($value), lock_ex); } public function get ($key) { $key = $this->escape_key($key); $file_name = $this->root_dir . '/' . $key; if (file_exists($file_name)) { return unserialize(file_get_contents($file_name)); } return null; } public function remove ($key) { $key = $this->escape_key($key); $file = $this->root_dir . '/' . $key; if (file_exists($file)) { unlink($file); } } public function remove_by_search ($key) { $key = $this->escape_key($key); $dir = $this->root_dir . '/' . $key; if (strrpos($key, '/') removedir($dir); } } private function escape_key ($key) { return str_replace('..', '', $key); } function removedir($dirname) { $result = false; $handle = opendir($dirname); while(($file = readdir($handle)) !== false) { if($file != '.' && $file != '..') { $dir = $dirname . directory_separator . $file; is_dir($dir) ? $this->removedir($dir) : unlink($dir); } } closedir($handle); rmdir($dirname) ? true : false; return $result; }}?>
其它类似信息

推荐信息