Permalink
Browse files

Adding some basic file caching mechanism

  • Loading branch information...
1 parent 3006164 commit 4539cef3014d2808253e8a841196e4f384ecb0cd randy committed Jul 28, 2011
Showing with 239 additions and 2 deletions.
  1. +143 −0 cache/file.php
  2. +27 −0 cache/icache.php
  3. +12 −0 config.php
  4. +1 −0 instaphp.php
  5. +18 −0 instaphpexception.php
  6. +38 −2 request.php
View
@@ -0,0 +1,143 @@
+<?php
+
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+namespace Instaphp\Cache
+{
+ use Instaphp;
+ use Instaphp\Config;
+ require_once('icache.php');
+ /**
+ * Description of file
+ *
+ * @author randy
+ */
+ class File implements iCache
+ {
+ private static $_instance;
+ private $_config;
+ private $_defaultTtl;
+ private $_path;
+
+ private final function __construct()
+ {
+ $this->_config = Config::Instance();
+ $cache = $this->_config->xpath("//Cache[@Engine='File']");
+
+ if (empty($cache) || count($cache) == 0 || !$cache[0]["Enabled"])
+ return false;
+ $this->_defaultTtl = strtotime("+2 minutes");
+ $this->_path = dirname(dirname(__FILE__)) . "/tmp/cache/";
+
+ if (!is_dir($this->_path))
+ mkdir($this->_path, 0755, true);
+
+ $this->Gc();
+ }
+
+ public static function Instance()
+ {
+ if (null == static::$_instance)
+ static::$_instance = new self();
+ return static::$_instance;
+ }
+
+ public function Set($key, $value, $ttl = NULL)
+ {
+ $ttl = (empty($ttl)) ? $this->_defaultTtl : strtotime($ttl);
+
+ if (!is_string($value))
+ $value = serialize($value);
+
+ $value = $ttl . PHP_EOL . $value;
+ $this->_save($key, $value);
+ }
+
+ public function Get($key)
+ {
+ if (null !== ($content = $this->_read($key)))
+ return $content;
+ return false;
+
+ }
+
+ public function Delete($key)
+ {
+ if ($this->Check($key))
+ @unlink($this->_path . $key);
+ }
+
+ public function Check($key)
+ {
+ return file_exists($this->_path . $key);
+ }
+
+ public function Clear()
+ {
+ $dir = dir($this->_path);
+ while (false !== ($file = $dir->read())) {
+ if ($file != "." && $file != "..")
+ $this->Delete($file);
+ }
+ }
+
+ public function Gc()
+ {
+ $min = (int)date("i");
+ $now = time();
+ if ($min % 2 == 0) {
+ $dir = dir($this->_path);
+ while (false !== ($file = $dir->read())) {
+ if ($file != "." && $file != "..") {
+ $lines = file($this->_path . $file);
+ $ttl = array_shift($lines);
+ if (!empty($ttl) && $now >= (int)$ttl)
+ $this->Delete($file);
+ }
+ }
+ }
+ }
+
+ private function _read($key)
+ {
+ $passes = 3;
+ $now = time();
+
+ if (!$this->Check($key))
+ return null;
+ $lines = null;
+ try
+ {
+ for ($i = $passes; $i >= 0; $i--) {
+ if (false !== ($lines = file($this->_path . $key)))
+ break;
+ sleep(1);
+ }
+ }
+ catch (Exception $ex) {}
+
+ if (null === $lines)
+ return null;
+
+ $ttl = (int)array_shift($lines);
+
+ $content = "";
+ if (count($lines) > 0) {
+ if ($now >= $ttl) {
+ $this->Delete($key);
+ return null;
+ }
+ $content = implode(PHP_EOL, $lines);
+ }
+
+ return @unserialize($content);
+ }
+
+ private function _save($key, $content)
+ {
+ return file_put_contents($this->_path . $key, $content, LOCK_EX|FILE_TEXT);
+ }
+ }
+}
View
@@ -0,0 +1,27 @@
+<?php
+
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+namespace Instaphp\Cache
+{
+ /**
+ *
+ * @author randy
+ */
+ interface iCache
+ {
+ public function Set($key, $obj, $ttl = null);
+
+ public function Get($key);
+
+ public function Delete($key);
+
+ public function Check($key);
+
+ public function Gc();
+
+ public function Clear();
+ }
+}
View
@@ -113,6 +113,18 @@ public function GetOAuthTokenUri()
return $this->Instagram->Endpoint . $this->Instagram->OAuthTokenPath;
}
+
+ public function CacheSetting($name, $key)
+ {
+ $cache = $this->xpath("//Instaphp/Cache[@Engine='File']");
+ if (empty($cache) || count($cache) == 0)
+ return null;
+
+ $cache = $cache[0];
+
+ return $cache->Settings->Setting[$key];
+
+ }
}
View
@@ -33,6 +33,7 @@
namespace Instaphp {
require_once('config.php');
+ require_once('cache/file.php');
require_once('webrequest.php');
require_once('request.php');
require_once('response.php');
View
@@ -0,0 +1,18 @@
+<?php
+
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+namespace Instaphp
+{
+ /**
+ * Description of instaphpexception
+ *
+ * @author randy
+ */
+ class InstaphpException extends \Exception
+ {
+ //put your code here
+ }
+}
View
@@ -33,6 +33,7 @@
namespace Instaphp {
use Instaphp\Config;
+ use Instaphp\Cache;
use Instaphp\WebRequest;
/**
* Request
@@ -64,6 +65,13 @@ class Request
*/
private $useCurl = false;
+ /**
+ *
+ * @var iCache Cache object used for caching
+ * @access private
+ */
+ private $_cache = null;
+
/**
* The constructor contructs
* @param string $url A URL in which to create a new request (optional)
@@ -74,6 +82,18 @@ public function __construct($url = null, $params = array())
$this->useCurl = self::HasCurl();
$this->parameters = $params;
$this->url = $url;
+
+ $cacheConfig = Config::Instance()->xpath("//Cache");
+ if (!empty($cacheConfig) && count($cacheConfig) > 0) {
+ $cacheConfig = $cacheConfig[0];
+ if ($cacheConfig["Enabled"]) {
+ $engine = (string)$cacheConfig["Engine"];
+ $method = new \ReflectionMethod("Instaphp\\Cache\\".$engine, 'Instance');
+ $this->_cache = $method->invoke(null, null);
+// $this->_cache = Cache\File::Instance();
+ }
+
+ }
}
@@ -90,8 +110,24 @@ public function Get($url = null, $params = array())
if (!empty($params))
$this->parameters = $params;
-
- $this->response = $this->GetResponse();
+ $query = '';
+ foreach ($this->parameters as $k => $v)
+ $query .= ((strlen ($query) == 0) ? '?' : '&') . sprintf('%s=%s', $k, $v);
+
+ if (null !== $this->_cache) {
+ $key = sha1($url.$query);
+
+ if (false === ($response = $this->_cache->Get($key))) {
+ $response = $this->GetResponse();
+ if (empty ($response->error)) {
+ $this->_cache->Set($key, $response);
+ }
+ }
+ } else {
+ $response = $this->GetResponse();
+ }
+
+ $this->response = $response;
return $this;
}

0 comments on commit 4539cef

Please sign in to comment.