/dev/shm php,基于liunx系统自带内存缓存/dev/shm的PHP缓存类

在《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是把内存模拟成文件系统,所以很容易就实现了层级式的缓存管理。这对合理利用内存空间是很有帮助的。比如一些针对用户的缓存,可以通过层级式的存储,在用户退出系统时全部移除。再比如一些同表的不同业务逻辑视图数据的缓存,在表更新后,也可以批量的移除。

class file_cache

{

private $root_dir;

public function __construct ($root_dir)

{

$this->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, '/') < 0)

$key .= '/';

if (file_exists($dir))

{

$this->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;

}

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值