Home >php教程 >php手册 >基于/dev/shm的PHP缓存类

基于/dev/shm的PHP缓存类

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-06 19:40:221829browse

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

<?php 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, '/') 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;
    }
}
?>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn