Unbe. cnphp_file_system_based_cache_class in "Socket communication between Erlang and PHP", I mentioned performance tests based on devshm cache implementation, here is a file system-based Cache class encapsulated in the test. in Linux, you only need to direct the root directory to devshm to change it to
Http://unbe.cn/php_file_system_based_cache_class/ in Erlang and PHP Socket communication, I mentioned the Performance Test Based on/dev/shm cache implementation, here is a file system-based Cache class encapsulated in the test. in Linux, you only need to direct the root directory to/dev/shm
Http://unbe.cn/php_file_system_based_cache_class/
In Socket communication between Erlang and PHP, I mentioned thatBased on/Dev/shmCacheThe performance test is implemented. Here we will share one of my encapsulatedBased onFile SystemCacheIn Linux, you only need to point the root directory to/dev/shmBased onMemoryCacheIn Windows, you can use a common file system for testing.
We need to remind you thatCacheClass is just a prototype. Just proposeBased on/Dev/shmCacheThe possibility of implementation is not completely usable in the production environment.CacheClass. It also has many areas to be improved and tested, such as the data expiration time function, data operations on the same key in concurrent cases, and batch removal and addition concurrency.
Example:
$ 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); // save data $ cache-> set ("user/2/data", $ data_2 ); // Save the data $ result = $ cache-> get ("user/1/data "); // get data $ cache-> remove ("user/1/data"); // delete data $ cache-> remove_by_search ("user", $ data_1 ); // delete all data under the user Node
Since/dev/shm simulates the memory into a file system, it is easy to implement hierarchicalCacheManagement. This is helpful for the rational use of memory space. For exampleCacheYou can use Hierarchical Storage to remove all data when you exit the system. For example, some data in different business logic views of the same tableCacheAfter updating the table, you can also remove it in batches.
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; }}?>