Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

IronCache as session store

  • Loading branch information...
commit f0ba687b4f8bdb90639fcf6cc5ecfbb33383ab49 1 parent d96cc36
@thousandsofthem thousandsofthem authored
Showing with 79 additions and 2 deletions.
  1. +63 −2 IronCache.class.php
  2. +16 −0 README.md
  3. BIN  iron_cache.phar
View
65 IronCache.class.php
@@ -5,7 +5,7 @@
* @link https://github.com/iron-io/iron_cache_php
* @link http://www.iron.io/products/cache
* @link http://dev.iron.io/
- * @version 0.0.2
+ * @version 0.1.0
* @package IronCache
* @copyright Feel free to copy, steal, take credit for, or whatever you feel like doing with this code. ;)
*/
@@ -106,7 +106,7 @@ public function asArray() {
}
class IronCache extends IronCore{
- protected $client_version = '0.0.2';
+ protected $client_version = '0.1.0';
protected $client_name = 'iron_cache_php';
protected $product_name = 'iron_cache';
protected $default_values = array(
@@ -118,6 +118,8 @@ class IronCache extends IronCore{
private $cache_name;
+ public $session_expire_time = 172800; # 2 days
+
/**
* @param string|array $config_file_or_options
* Array of options or name of config file.
@@ -333,6 +335,65 @@ public function increment($key, $amount = 1){
return $this->incrementItem($this->cache_name, $key, $amount);
}
+
+ function session_open($savePath, $sessionName){
+ $this->setCacheName($sessionName);
+ return true;
+ }
+
+ function session_close(){
+ return true;
+ }
+
+ function session_read($id){
+ $item = $this->get($id);
+ if ($item !== null) {
+ return $item->value;
+ } else {
+ return null;
+ }
+ }
+
+ function session_write($id, $data){
+ $this->put($id, array(
+ "value" => $data,
+ "expires_in" => $this->session_expire_time
+ ));
+ return true;
+ }
+
+ function session_destroy($id){
+ try {
+ $this->delete($id);
+ } catch (Exception $e) {}
+ return true;
+ }
+
+ function session_gc($maxlifetime){
+ # auto-expire by default, no need for gc
+ return true;
+ }
+
+ /**
+ * Set IronCache as session store handler
+ *
+ * @param null|integer $session_expire_time Expire time in seconds
+ */
+ function set_as_session_store($session_expire_time = null){
+ if ($session_expire_time != null){
+ $this->session_expire_time = $session_expire_time;
+ }
+ session_set_save_handler(
+ array($this, 'session_open'),
+ array($this, 'session_close'),
+ array($this, 'session_read'),
+ array($this, 'session_write'),
+ array($this, 'session_destroy'),
+ array($this, 'session_gc')
+ );
+ }
+
+
/* PRIVATE FUNCTIONS */
private static function encodeCache($cache){
View
16 README.md
@@ -104,6 +104,22 @@ Select cache before interacting with items
* By method: `$cache->setCacheName('my_cache');`
* Do it later when you need: `$cache->getItem('my_cache','my_key');`
+
+Using IronCache as session store
+===============
+
+
+```php
+<?php
+$cache = new IronCache();
+$cache->set_as_session_store();
+
+# Use session as usual
+session_start();
+...
+
+```
+
Troubleshooting
===============
View
BIN  iron_cache.phar
Binary file not shown
Please sign in to comment.
Something went wrong with that request. Please try again.