• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP Memcached + APC + 文件缓存封装实现代码_php技巧

php 搞代码 3年前 (2022-01-26) 19次浏览 已收录 0个评论

使用方法:
Memcached

 <BR>$cache = new Cache_MemCache(); <BR>$cache->addServer('www1'); <BR>$cache->addServer('www2',11211,20); // this server has double the memory, and gets double the weight <BR>$cache->addServer('www3',11211); <BR>// Store some data in the cache for 10 minutes <BR>$cache->store('my_key','foobar',600); <BR>// Get it out of the cache again <BR>echo($cache->fetch('my_key')); <BR>


文件缓存

 <BR>$cache = new Cache_File(); <BR>$key = 'getUsers:selectAll'; <BR>// check if the data is not in the cache already <BR>if (!$data = $cache->fetch($key)) { <BR>// assuming there is a database connection <BR>$result = mysql_query("SELECT * FROM users"); <BR>$data = array(); <BR>// fetching all the data and putting it in an array <BR>while($row = mysql_fetch_assoc($result)) { $data[] = $row; } <BR>// Storing the data in the cache for 10 minutes <BR>$cache->store($key,$data,600); <BR>} <BR>


下载: class_cache3.php

<?php <br><br>abstract class Cache_Abstract { <BR>abstract function fetch($key); <BR>abstract function store($key, $data, $ttl); <BR>abstract function delete($key); <BR>} <br><br>class Cache_APC extends Cache_Abstract { <br><br>function fetch($key) { <BR>return apc_fetch($key); <BR>} <br><br>function store($key, $data, $ttl) { <BR>return apc_store($key, $data, $ttl); <BR>} <br><br>function delete($key) { <BR>return apc_delete($key); <BR>} <br><br>} <br><br>class Cache_MemCache extends Cache_Abstract { <BR>public $connection; <br><br>function __construct() { <BR>$this->connection = new MemCache; <BR>} <br><br>function store($key, $data, $ttl) { <BR>return $this->connection->set($key, $data, 0, $ttl); <BR>} <br><br>function fetch($key) { <BR>return $this->connection->get($key); <BR>} <br><br>function delete($key) { <BR>return $this->connection->delete($key); <BR>} <br><br>function addServer($host, $port = 11211, $weight = 10) { <BR>$this->connection->addServer($host, $port, true, $weight); <BR>} <br><br>} <br><br>class Cache_File extends Cache_Abstract { <br><br>function store($key, $data, $ttl) { <BR>$h = fopen($this->getFileName($key), 'a+'); <BR>if (!$h) <BR>throw new Exception('Could not write to cache'); <BR>flock($h, LOCK_EX); <BR>fseek($h, 0); <BR>ftruncate($h, 0); <BR>$data = serialize(array(time() + $ttl, $data)); <BR>if (fwrite($h, $data) === false) { <BR>throw new Exception('Could not write to cache'); <BR>} <BR>fclose($h); <BR>} <br><br>function fetch($key) { <BR>$filename = $this->getFileName($key); <BR>if (!file_exists($filename)) <BR>return false; <BR>$h = fopen($filename, 'r'); <BR>if (!$h) <BR>return false; <BR>flock($h, LOCK_SH); <BR>$data = file_get_contents($filename); <BR>fclose($h); <BR>$data = @ unserialize($data); <BR>if (!$data) { <BR>unlink($filename); <BR>return false; <BR>} <BR>if (time() > $data[0]) { <BR>unlink($filename); <BR>return false; <BR>} <BR>return $data[1]; <BR>} <br><br>function delete($key) { <BR>$filename = $this->getFileName($key); <BR>if (file_exists($filename)) { <BR>return unlink($filename); <BR>} <BR>else { <BR>return false; <BR>} <BR>} <br><br>private function getFileName($<b style="color:transparent">来&源gao@dai!ma.com搞$代^码%网</b><img>搞gaodaima代码</img>key) { <BR>return '/tmp/s_cache' . md5($key); <BR>} <br><br>} <BR>?><BR>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP Memcached + APC + 文件缓存封装实现代码_php技巧
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址