Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 17 commits
  • 7 files changed
  • 1 commit comment
  • 4 contributors
Showing with 242 additions and 95 deletions.
  1. +183 −65 IronCache.class.php
  2. +16 −0 README.md
  3. +36 −23 TestCache.php
  4. +3 −3 build.php
  5. +4 −3 composer.json
  6. BIN iron_cache.phar
  7. +0 −1 sample_config.ini
View
248 IronCache.class.php
@@ -5,19 +5,20 @@
* @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.3
* @package IronCache
* @copyright Feel free to copy, steal, take credit for, or whatever you feel like doing with this code. ;)
*/
-class IronCache_Item {
- private $value;
- private $expires_in;
- private $replace;
- private $add;
+class IronCache_Item
+{
+ protected $value;
+ protected $expires_in;
+ protected $replace;
+ protected $add;
- const max_expires_in = 2592000;
+ const MAX_EXPIRES_IN = 2592000;
/**
* Create a new item.
@@ -28,85 +29,97 @@ class IronCache_Item {
* Required:
* - value: string - The item data, as a string.
* Optional:
- * - expires_in: integer - How long in seconds to keep the item on the cache before it is deleted. Default is 604,800 seconds (7 days). Maximum is 2,592,000 seconds (30 days).
+ * - expires_in: integer - How long in seconds to keep the item on the cache before it is deleted.
+ * Default is 604,800 seconds (7 days). Maximum is 2,592,000 seconds (30 days).
* - replace: boolean - Will only work if key already exists.
* - add: boolean - Will only work if key does not exist.
*/
- function __construct($item) {
- if(is_string($item) || is_integer($item)) {
+ public function __construct($item)
+ {
+ if (is_string($item) || is_integer($item)) {
$this->setValue($item);
- } elseif(is_array($item)) {
+ } elseif (is_array($item)) {
$this->setValue($item['value']);
- if(array_key_exists("replace", $item)) {
+ if (array_key_exists("replace", $item)) {
$this->setReplace($item['replace']);
}
- if(array_key_exists("add", $item)) {
+ if (array_key_exists("add", $item)) {
$this->setAdd($item['add']);
}
- if(array_key_exists("expires_in", $item)) {
+ if (array_key_exists("expires_in", $item)) {
$this->setExpiresIn($item['expires_in']);
}
}
}
- public function setValue($value) {
- if(empty($value)) {
+ public function setValue($value)
+ {
+ if ($value === null) {
throw new InvalidArgumentException("Please specify a value");
} else {
$this->value = $value;
}
}
- public function getValue() {
+ public function getValue()
+ {
return $this->value;
}
- public function setReplace($replace) {
+ public function setReplace($replace)
+ {
$this->replace = $replace;
}
- public function getReplace() {
+ public function getReplace()
+ {
return $this->replace;
}
- public function setAdd($add) {
+ public function setAdd($add)
+ {
$this->add = $add;
}
- public function getAdd() {
+ public function getAdd()
+ {
return $this->add;
}
- public function setExpiresIn($expires_in) {
- if($expires_in > self::max_expires_in) {
- throw new InvalidArgumentException("Expires In can't be greater than ".self::max_expires_in.".");
+ public function setExpiresIn($expires_in)
+ {
+ if ($expires_in > self::MAX_EXPIRES_IN) {
+ throw new InvalidArgumentException("Expires In can't be greater than ".self::MAX_EXPIRES_IN.".");
} else {
$this->expires_in = $expires_in;
}
}
- public function getExpiresIn(){
+ public function getExpiresIn()
+ {
return $this->expires_in;
}
- public function asArray() {
+ public function asArray()
+ {
$array = array();
$array['value'] = $this->getValue();
- if($this->getExpiresIn() != null) {
+ if ($this->getExpiresIn() != null) {
$array['expires_in'] = $this->getExpiresIn();
}
- if($this->getReplace() != null) {
+ if ($this->getReplace() != null) {
$array['replace'] = $this->getReplace();
}
- if($this->getAdd() != null) {
+ if ($this->getAdd() != null) {
$array['add'] = $this->getAdd();
}
return $array;
}
}
-class IronCache extends IronCore{
- protected $client_version = '0.0.2';
+class IronCache extends IronCore
+{
+ protected $client_version = '0.1.3';
protected $client_name = 'iron_cache_php';
protected $product_name = 'iron_cache';
protected $default_values = array(
@@ -116,10 +129,12 @@ class IronCache extends IronCore{
'api_version' => '1',
);
- private $cache_name;
+ protected $cache_name;
+
+ public $session_expire_time = 172800; # 2 days
/**
- * @param string|array $config_file_or_options
+ * @param string|array $config
* Array of options or name of config file.
* Fields in options array or in config:
*
@@ -133,8 +148,9 @@ class IronCache extends IronCore{
* - api_version
* @param string|null $cache_name set default cache name
*/
- function __construct($config_file_or_options = null, $cache_name = null){
- $this->getConfigData($config_file_or_options);
+ public function __construct($config = null, $cache_name = null)
+ {
+ $this->getConfigData($config);
$this->url = "{$this->protocol}://{$this->host}:{$this->port}/{$this->api_version}/";
$this->setCacheName($cache_name);
}
@@ -145,11 +161,12 @@ function __construct($config_file_or_options = null, $cache_name = null){
* @param string $project_id Project ID
* @throws InvalidArgumentException
*/
- public function setProjectId($project_id) {
- if (!empty($project_id)){
+ public function setProjectId($project_id)
+ {
+ if (!empty($project_id)) {
$this->project_id = $project_id;
}
- if (empty($this->project_id)){
+ if (empty($this->project_id)) {
throw new InvalidArgumentException("Please set project_id");
}
}
@@ -160,17 +177,19 @@ public function setProjectId($project_id) {
* @param string $cache_name name of cache
* @throws InvalidArgumentException
*/
- public function setCacheName($cache_name) {
- if (!empty($cache_name)){
+ public function setCacheName($cache_name)
+ {
+ if (!empty($cache_name)) {
$this->cache_name = $cache_name;
}
}
- public function getCaches($page = 0){
+ public function getCaches($page = 0)
+ {
$url = "projects/{$this->project_id}/caches";
$params = array();
- if($page > 0) {
+ if ($page > 0) {
$params['page'] = $page;
}
$this->setJsonHeaders();
@@ -184,7 +203,8 @@ public function getCaches($page = 0){
* @param string $cache
* @return mixed
*/
- public function getCache($cache) {
+ public function getCache($cache)
+ {
$cache = self::encodeCache($cache);
$url = "projects/{$this->project_id}/caches/$cache";
$this->setJsonHeaders();
@@ -212,12 +232,13 @@ public function getCache($cache) {
*
* @return mixed
*/
- public function putItem($cache, $key, $item) {
+ public function putItem($cache, $key, $item)
+ {
$cache = self::encodeCache($cache);
$key = self::encodeKey($key);
- $itm = new IronCache_Item($item);
- $req = $itm->asArray();
- $url = "projects/{$this->project_id}/caches/$cache/items/$key";
+ $itm = new IronCache_Item($item);
+ $req = $itm->asArray();
+ $url = "projects/{$this->project_id}/caches/$cache/items/$key";
$this->setJsonHeaders();
$res = $this->apiCall(self::PUT, $url, $req);
@@ -232,7 +253,8 @@ public function putItem($cache, $key, $item) {
* @return mixed|null single item or null
* @throws Http_Exception
*/
- public function getItem($cache, $key) {
+ public function getItem($cache, $key)
+ {
$cache = self::encodeCache($cache);
$key = self::encodeKey($key);
$url = "projects/{$this->project_id}/caches/$cache/items/$key";
@@ -240,17 +262,18 @@ public function getItem($cache, $key) {
$this->setJsonHeaders();
try {
$res = $this->apiCall(self::GET, $url);
- }catch (Http_Exception $e){
- if ($e->getCode() == Http_Exception::NOT_FOUND){
+ } catch (Http_Exception $e) {
+ if ($e->getCode() == Http_Exception::NOT_FOUND) {
return null;
- }else{
+ } else {
throw $e;
}
}
return self::json_decode($res);
}
- public function deleteItem($cache, $key) {
+ public function deleteItem($cache, $key)
+ {
$cache = self::encodeCache($cache);
$key = self::encodeKey($key);
$url = "projects/{$this->project_id}/caches/$cache/items/$key";
@@ -271,7 +294,8 @@ public function deleteItem($cache, $key) {
* @param int $amount Change by this value
* @return mixed|void
*/
- public function incrementItem($cache, $key, $amount = 1){
+ public function incrementItem($cache, $key, $amount = 1)
+ {
$cache = self::encodeCache($cache);
$key = self::encodeKey($key);
$url = "projects/{$this->project_id}/caches/$cache/items/$key/increment";
@@ -291,7 +315,8 @@ public function incrementItem($cache, $key, $amount = 1){
* @return mixed|null
* @throws InvalidArgumentException
*/
- public function get($key){
+ public function get($key)
+ {
return $this->getItem($this->cache_name, $key);
}
@@ -304,7 +329,8 @@ public function get($key){
* @return mixed
* @throws InvalidArgumentException
*/
- public function put($key, $item){
+ public function put($key, $item)
+ {
return $this->putItem($this->cache_name, $key, $item);
}
@@ -316,7 +342,8 @@ public function put($key, $item){
* @return mixed|void
* @throws InvalidArgumentException
*/
- public function delete($key){
+ public function delete($key)
+ {
return $this->deleteItem($this->cache_name, $key);
}
@@ -329,34 +356,125 @@ public function delete($key){
* @return mixed|void
* @throws InvalidArgumentException
*/
- public function increment($key, $amount = 1){
+ public function increment($key, $amount = 1)
+ {
return $this->incrementItem($this->cache_name, $key, $amount);
}
+ /**
+ * Clear a Cache
+ * Delete all items in a cache. This cannot be undone.
+ *
+ * @param string|null $cache Cache name or null
+ * @return mixed
+ */
+ public function clear($cache = null)
+ {
+ if ($cache === null) {
+ $cache = $this->cache_name;
+ }
+ $cache = self::encodeCache($cache);
+ $url = "projects/{$this->project_id}/caches/$cache/clear";
+ $params = array();
+ $this->setJsonHeaders();
+ return self::json_decode($this->apiCall(self::POST, $url, $params));
+ }
+
+
+ public function session_open($savePath, $sessionName)
+ {
+ $this->setCacheName($sessionName);
+ return true;
+ }
+
+ public function session_close()
+ {
+ return true;
+ }
+
+ public function session_read($id)
+ {
+ $item = $this->get($id);
+ if ($item !== null) {
+ return $item->value;
+ } else {
+ return null;
+ }
+ }
+
+ public function session_write($id, $data)
+ {
+ $this->put($id, array(
+ "value" => $data,
+ "expires_in" => $this->session_expire_time
+ ));
+ return true;
+ }
+
+ public function session_destroy($id)
+ {
+ try {
+ $this->delete($id);
+ } catch (Exception $e) {
+ # ignore any exceptions
+ }
+ return true;
+ }
+
+ public 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
+ */
+ public 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){
- if (empty($cache)){
+ protected static function encodeCache($cache)
+ {
+ if (empty($cache)) {
throw new InvalidArgumentException('Please set $cache variable');
}
return rawurlencode($cache);
}
- private static function encodeKey($key){
- if (empty($key)){
+ protected static function encodeKey($key)
+ {
+ if (empty($key)) {
throw new InvalidArgumentException('Please set $key variable');
}
return rawurlencode($key);
}
- private function setJsonHeaders(){
+ protected function setJsonHeaders()
+ {
$this->setCommonHeaders();
}
- private function setPostHeaders(){
+ protected function setPostHeaders()
+ {
$this->setCommonHeaders();
$this->headers['Content-Type'] ='multipart/form-data';
}
-
-}
+}
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
59 TestCache.php
@@ -1,36 +1,49 @@
<?php
-include("../iron_core_php/IronCore.class.php");
-include("IronCache.class.php");
+#require "iron_cache.phar";
+require("../iron_core_php/IronCore.class.php");
+require("IronCache.class.php");
$cache = new IronCache();
$cache->ssl_verifypeer = false;
#$cache->debug_enabled = true;
-$cache->setCacheName('cache #1');
-$key = "key #1";
+$cache->setCacheName('cache #4');
-echo "Put item on cache:";
-$res = $cache->put($key, 777);
-print_r($res);
-echo "\nGet item from cache:";
-$item = $cache->get($key);
-print_r($item);
+for ($i = 0; $i < 10; $i++) {
+ $key = "key ##$i";
-echo "Increment item on cache:";
-$res = $cache->increment($key, -222);
-print_r($res);
+ echo "Put item on cache:\n";
+ $res = $cache->put($key, 777);
+ var_dump($res);
-echo "\nGet item from cache:";
-$item = $cache->get($key);
-print_r($item);
+ echo "\nGet item from cache:\n";
+ $item = $cache->get($key);
+ var_dump($item);
-echo "\nRemoving item from cache:";
-$res = $cache->delete($key);
-print_r($res);
+ echo "Increment item on cache:\n";
+ $res = $cache->increment($key, -222);
+ var_dump($res);
-echo "\nGet item from cache:";
-$item = $cache->get($key);
-var_dump($item);
+ echo "\nGet item from cache:\n";
+ $item = $cache->get($key);
+ var_dump($item);
-echo "\nEnd\n";
+ echo "\nRemoving item from cache:\n";
+ $res = $cache->delete($key);
+ var_dump($res);
+
+ echo "\nGet item from cache:\n";
+ $item = $cache->get($key);
+ var_dump($item);
+
+ echo "\nClear cache:\n";
+ $res = $cache->clear();
+ var_dump($res);
+
+
+ echo "----$i----\n";
+}
+
+
+echo "\n done";
View
6 build.php
@@ -21,14 +21,14 @@
__HALT_COMPILER(); ?>');
# Files
-$phar->addFile('../iron_core_php/IronCore.class.php','IronCore.class.php');
+$phar->addFile('../iron_core_php/IronCore.class.php', 'IronCore.class.php');
$phar->addFile('IronCache.class.php');
$phar->addFile('LICENSE', 'LICENSE');
-echo "\ndone - ".(round(filesize('iron_cache.phar')/1024,2))." KB\n";
+echo "\ndone - ".(round(filesize('iron_cache.phar')/1024, 2))." KB\n";
# Verification
require "phar://iron_cache.phar";
$cache = new IronCache();
-echo "Build finished successfully\n";
+echo "Build finished successfully\n";
View
7 composer.json
@@ -2,7 +2,7 @@
"name": "iron-io/iron_cache",
"type": "library",
"description": "Client binding for IronCache (A key/value store in the cloud)",
- "keywords": ["cache","iron_cache","iron","store"],
+ "keywords": ["cache","iron_cache","iron","store","cloud storage"],
"homepage": "http://github.com/iron-io/iron_cache_php",
"license": "MIT",
"authors": [
@@ -14,10 +14,11 @@
}
],
"require": {
- "php": ">=5.2.0"
+ "php": ">=5.2.0",
+ "iron-io/iron_core": "0.*"
},
"autoload": {
- "files": ["iron_cache.phar"]
+ "files": ["IronCache.class.php"]
},
"repositories":[
{
View
BIN iron_cache.phar
Binary file not shown.
View
1 sample_config.ini
@@ -6,4 +6,3 @@ project_id = ENTER_YOUR_PROJECT_ID_HERE
; api_version = 1
; host = cache-aws-us-east-1.iron.io
; port = 443
-

Showing you all comments on commits in this comparison.

@br0sk

Good move now the auto loading works properly. Thanks for considering my pull request.

Something went wrong with that request. Please try again.