Post by billynoah » Mon Sep 01, 2014 6:12 am

A few functions for caching in memory instead of disk.. very helpful for large databases where disk caching can be nearly as slow as simply querying the db:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<modification>
	<id>memcache functions</id>
	<version>1.2</version>
	<vqmver>2.4.0</vqmver>
	<author>billynoah@gmail.com</author>
	
	<file name="system/library/cache.php">
		<operation>
			<search position="before"><![CDATA[public function get($key)]]></search>
			<add><![CDATA[
	private $mexpire = 3600;
	
	public function mget($key) {
		$m = new Memcached();
		$m->addServer('localhost', 11211);
		return $m->get($key);
	}
	
	public function mset($key, $value) {
		$m = new Memcached();
		$m->addServer('localhost', 11211, (time() + $this->mexpire));
		return $m->set($key, $value);
	}
	
	public function mdelete($key) {
		$m = new Memcached();
		$m->addServer('localhost', 11211);
		$m->delete($key);
	}
	
	// get all keys or get keys containing $prefix
	public function mgetkeys($prefix = false) {
		$m = new Memcached();
		$m->addServer('localhost', 11211);
		$keys = $m->getAllKeys();
		if ($prefix) {
			foreach ($keys as $index => $key) {
				if (strpos($key,$prefix) === false) { unset($keys[$index]); }
			}
		}
		return $keys;
	}
	
	// delete all keys or delete keys containing $prefix
	public function mdeletekeys($prefix = false) {
		$m = new Memcached();
		$m->addServer('localhost', 11211);
		$keys = $m->getAllKeys();
		if ($prefix !== false) {
			foreach ($keys as $index => $key) {
				if (strpos($key,$prefix) === false) {
					unset($keys[$index]);
				} else {
					$m->delete($key);
				}
			}
		} else {
			$m->flush();
		}
		return $keys;
	}
			
			]]></add>
		</operation>
	</file>

</modification>

Image


User avatar
Active Member

Posts

Joined
Tue Jan 15, 2013 12:46 pm
Who is online

Users browsing this forum: No registered users and 5 guests