Last active
August 29, 2015 14:06
-
-
Save enijar/747c768212c1f9b88569 to your computer and use it in GitHub Desktop.
Memcache Helper Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Long Version
Memcache Install guide
short & Sweet Version
Install
Config File
Default host: 127.0.0.1
Default port: 11211
Default memory: 64MB
Install Memcache with pecl
If pecl didn't work, then try this and try again:
Update php.ini
Add
extension=memcache.so
to the bottom of the file