Created
May 9, 2014 05:43
-
-
Save nmabhinandan/0fe1968be81b403b2225 to your computer and use it in GitHub Desktop.
Using Azure Table storage as Larave cache
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 | |
use Illuminate\Cache\StoreInterface; | |
use Illuminate\Config; | |
use WindowsAzure\Common\ServicesBuilder; | |
use WindowsAzure\Common\ServiceException; | |
use WindowsAzure\Table\Models\Entity; | |
use WindowsAzure\Table\Models\EdmType; | |
class AzureTableStore implements StoreInterface{ | |
protected $prefix; | |
function __construct($prefix = ''){ | |
$this->prefix = $prefix; | |
$this->connectionString = 'DefaultEndpointsProtocol='.(strlen(Config::get('cache.azure.DefaultEndpointsProtocol'))>0)?Config::get('cache.azure.DefaultEndpointsProtocol'):'http' | |
.';AccountName='.(strlen(Config::get('cache.azure.StorageAccountName'))>0)?Config::get('cache.azure.StorageAccountName'):null | |
.';AccountKey='.(strlen(Config::get('cache.azure.StorageAccountKey'))>0)?Config::get('cache.azure.StorageAccountKey'):null.(Config::get('cache.azure.UseDevelopmentStorage'))?'UseDevelopmentStorage=true':''; | |
$this->partitionKey = (strlen(Config::get('cache.azure.partitionKey')>0))?Config::get('cache.azure.partitionKey'):'cache'; | |
$this->table = (strlen(Config::get("cache.azure.")>0))?Config::get("cache.azure.table"):'cache'; | |
} | |
/** | |
* Retrieve an item from the cache by key. | |
* | |
* @param string $key | |
* @return mixed | |
*/ | |
public function get($key) | |
{ | |
$tableRestProxy = ServicesBuilder::getInstance()->createTableService($this->connectionString); | |
try { | |
$result = $tableRestProxy->getEntity($this->table, $this->partitionKey, $key); | |
} | |
catch(ServiceException $e){ | |
throw $e; | |
} | |
$entity = $result->getEntity(); | |
$expiration = $entity->getPropertyValue("expiration"); | |
if(time()>=$expiration){ | |
return $this->forget($key); | |
} | |
return $entity->getPropertyValue("value"); | |
} | |
/** | |
* Store an item in the cache for a given number of minutes. | |
* | |
* @param string $key | |
* @param mixed $value | |
* @param int $minutes | |
* @return void | |
*/ | |
public function put($key, $value, $minutes) { | |
$tableRestProxy = $this->servicesBuilder->createTableService($this->connectionString); | |
if(!$this->createTableIfNotExists($tableRestProxy)){ | |
throw new \Exception('Cannot create table '.Config::get('cache.table')); | |
} | |
$expireation = time()+($minutes*60); | |
$entity = new Entity(); | |
$entity->setPartitionKey($this->partitionKey); | |
$entity->setRowKey($key); | |
$entity->addProperty("value",EdmType::STRING,serialize($value)); | |
$entity->addProperty("expiration",EdmType::INT64,$expireation); | |
try{ | |
$tableRestProxy->insertOrMergeEntity($this->table, $entity); | |
} | |
catch(ServiceException $e){ | |
throw new $e; | |
} | |
} | |
/** | |
* Increment the value of an item in the cache. | |
* | |
* @param string $key | |
* @param mixed $value | |
* @return void | |
*/ | |
public function increment($key, $value = 1) | |
{ | |
throw new \LogicException("Increment operations not supported by this driver."); | |
} | |
/** | |
* Decrement the value of an item in the cache. | |
* | |
* @param string $key | |
* @param mixed $value | |
* @return void | |
*/ | |
public function decrement($key, $value = 1) | |
{ | |
throw new \LogicException("Increment operations not supported by this driver."); | |
} | |
/** | |
* Store an item in the cache indefinitely. | |
* | |
* @param string $key | |
* @param mixed $value | |
* @return void | |
*/ | |
public function forever($key, $value) | |
{ | |
$tableRestProxy = $this->servicesBuilder->createTableService($this->connectionString); | |
if(!$this->createTableIfNotExists($tableRestProxy)){ | |
throw new \Exception('Cannot create table '.Config::get('cache.table')); | |
} | |
$entity = new Entity(); | |
$entity->setPartitionKey($this->partitionKey); | |
$entity->setRowKey($key); | |
$entity->addProperty("value",EdmType::STRING,serialize($value)); | |
try{ | |
$tableRestProxy->insertOrMergeEntity($this->table, $entity); | |
} | |
catch(ServiceException $e){ | |
throw new $e; | |
} | |
} | |
/** | |
* Remove an item from the cache. | |
* | |
* @param string $key | |
* @return void | |
*/ | |
public function forget($key) | |
{ | |
$tableRestProxy = $this->servicesBuilder->createTableService($this->connectionString); | |
try { | |
// Delete entity. | |
$tableRestProxy->deleteEntity($this->table, $this->partitionKey, $key); | |
} | |
catch(ServiceException $e){ | |
throw $e; | |
} | |
} | |
/** | |
* Remove all items from the cache. | |
* | |
* @return void | |
*/ | |
public function flush() | |
{ | |
$tableRestProxy = $this->servicesBuilder->createTableService($this->connectionString); | |
try{ | |
$tableRestProxy->deleteTable($this->table); | |
$tableRestProxy->createTable($this->table); | |
} | |
catch(ServiceException $e){ | |
throw $e; | |
} | |
} | |
/** | |
* Get the cache key prefix. | |
* | |
* @return string | |
*/ | |
public function getPrefix() | |
{ | |
return $this->prefix; | |
} | |
private function createTableIfNotExists($tableRestProxy) { | |
try { | |
$tableRestProxy->createTable($this->table); | |
} | |
catch(ServiceException $e){ | |
$code = $e->getCode(); | |
return ($code === 409)?true:false; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment