Skip to content

Instantly share code, notes, and snippets.

@enijar
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enijar/747c768212c1f9b88569 to your computer and use it in GitHub Desktop.
Save enijar/747c768212c1f9b88569 to your computer and use it in GitHub Desktop.
Memcache Helper Class
<?php
/**
* Helper class for storing and fetching cache
* data by passing through keys. Setting up
* Memcache tutorial: http://code.tutsplus.com/tutorials/turbocharge-your-website-with-memcached--net-23939
*/
class Cache
{
protected $memcache;
public function __construct()
{
$this->memcache = new Memcache;
$this->memcache->connect(MEMCACHED_HOST, MEMCACHED_PORT);
}
public function store($key, $values = [])
{
$this->memcache->set($key, $values);
}
public function fetch($key)
{
$cache = $this->memcache->get($key);
if($cache) return $cache;
return false;
}
}
<?php
requre_once 'Cache.php';
$cache = new Cache;
$cache->store('key_id_1', [
'id' => 1,
'name' => 'Table',
'description' => 'Wooden table with four working legs',
'price' => 9.99
]);
echo '<pre>', print_r($cache->fetch('key_id_1'), true);
@enijar
Copy link
Author

enijar commented Sep 20, 2014

Long Version
Memcache Install guide

short & Sweet Version

Install

sudo apt-get install memcached

Config File

/etc/memcached.conf

Default host: 127.0.0.1
Default port: 11211
Default memory: 64MB

Install Memcache with pecl

sudo pecl install memcache

If pecl didn't work, then try this and try again:

sudo apt-get install php5-dev

Update php.ini
Add extension=memcache.so to the bottom of the file

sudo nano /etc/php5/apache2/php.ini

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment