Usage:
Memcached
$cache = new Cache_MemCache(); $cache->addServer('www1'); $cache->addServer('www2',11211,20); // this server has double the memory, and gets double the weight $cache->addServer('www3',11211); // Store some data in the cache for 10 minutes $cache->store('my_key','foobar',600); // Get it out of the cache again echo($cache->fetch('my_key'));
File cache
$cache = new Cache_File(); $key = 'getUsers:selectAll'; // check if the data is not in the cache already if (!$data = $cache->fetch($key)) { // assuming there is a database connection $result = mysql_query("SELECT * FROM users"); $data = array(); // fetching all the data and putting it in an array while($row = mysql_fetch_assoc($result)) { $data[] = $row; } // Storing the data in the cache for 10 minutes $cache->store($key,$data,600); }
Download: class_cache3.php
<?php abstract class Cache_Abstract { abstract function fetch($key); abstract function store($key, $data, $ttl); abstract function delete($key); } class Cache_APC extends Cache_Abstract { function fetch($key) { return apc_fetch($key); } function store($key, $data, $ttl) { return apc_store($key, $data, $ttl); } function delete($key) { return apc_delete($key); } } class Cache_MemCache extends Cache_Abstract { public $connection; function __construct() { $this->connection = new MemCache; } function store($key, $data, $ttl) { return $this->connection->set($key, $data, 0, $ttl); } function fetch($key) { return $this->connection->get($key); } function delete($key) { return $this->connection->delete($key); } function addServer($host, $port = 11211, $weight = 10) { $this->connection->addServer($host, $port, true, $weight); } } class Cache_File extends Cache_Abstract { function store($key, $data, $ttl) { $h = fopen($this->getFileName($key), 'a+'); if (!$h) throw new Exception('Could not write to cache'); flock($h, LOCK_EX); fseek($h, 0); ftruncate($h, 0); $data = serialize(array(time() + $ttl, $data)); if (fwrite($h, $data) === false) { throw new Exception('Could not write to cache'); } fclose($h); } function fetch($key) { $filename = $this->getFileName($key); if (!file_exists($filename)) return false; $h = fopen($filename, 'r'); if (!$h) return false; flock($h, LOCK_SH); $data = file_get_contents($filename); fclose($h); $data = @ unserialize($data); if (!$data) { unlink($filename); return false; } if (time() > $data[0]) { unlink($filename); return false; } return $data[1]; } function delete($key) { $filename = $this->getFileName($key); if (file_exists($filename)) { return unlink($filename); } else { return false; } } private function getFileName($key) { return '/tmp/s_cache' . md5($key); } } ?>
More PHP Memcached + APC + file cache encapsulation implementation code For related articles, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.