CC-5555 : Implement simple caching for preferences
using apc to save/fetch preferences to/from a cache.
This commit is contained in:
parent
050d984882
commit
f45708682f
3 changed files with 60 additions and 12 deletions
26
airtime_mvc/application/models/Cache.php
Normal file
26
airtime_mvc/application/models/Cache.php
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class Cache
|
||||||
|
{
|
||||||
|
|
||||||
|
private function createCacheKey($key, $userId = null) {
|
||||||
|
|
||||||
|
$CC_CONFIG = Config::getConfig();
|
||||||
|
$a = $CC_CONFIG["apiKey"][0];
|
||||||
|
$cacheKey = "{$key}{$userId}{$a}";
|
||||||
|
|
||||||
|
return $cacheKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function store($key, $value, $userId = null) {
|
||||||
|
|
||||||
|
$cacheKey = self::createCacheKey($key, $userId);
|
||||||
|
return apc_store($cacheKey, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function fetch($key, $userId = null) {
|
||||||
|
|
||||||
|
$cacheKey = self::createCacheKey($key, $userId);
|
||||||
|
return apc_fetch($cacheKey);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
require_once 'Cache.php';
|
||||||
|
|
||||||
class Application_Model_Preference
|
class Application_Model_Preference
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -10,6 +12,8 @@ class Application_Model_Preference
|
||||||
*/
|
*/
|
||||||
private static function setValue($key, $value, $isUserValue = false, $userId = null)
|
private static function setValue($key, $value, $isUserValue = false, $userId = null)
|
||||||
{
|
{
|
||||||
|
$cache = new Cache();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$con = Propel::getConnection(CcPrefPeer::DATABASE_NAME);
|
$con = Propel::getConnection(CcPrefPeer::DATABASE_NAME);
|
||||||
$con->beginTransaction();
|
$con->beginTransaction();
|
||||||
|
@ -103,10 +107,29 @@ class Application_Model_Preference
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$cache->store($key, $value, $userId);
|
||||||
|
Logging::info("SAVING {$key} {$userId} into cache. = {$value}");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function getValue($key, $isUserValue = false)
|
private static function getValue($key, $isUserValue = false)
|
||||||
{
|
{
|
||||||
|
$cache = new Cache();
|
||||||
|
|
||||||
|
//For user specific preference, check if id matches as well
|
||||||
|
$userId = null;
|
||||||
|
if ($isUserValue) {
|
||||||
|
$auth = Zend_Auth::getInstance();
|
||||||
|
if ($auth->hasIdentity()) {
|
||||||
|
$userId = $auth->getIdentity()->id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$res = $cache->fetch($key, $userId);
|
||||||
|
if ($res !== false) {
|
||||||
|
Logging::info("returning {$key} {$userId} from cache. = {$res}");
|
||||||
|
return $res;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
//Check if key already exists
|
//Check if key already exists
|
||||||
|
@ -117,14 +140,10 @@ class Application_Model_Preference
|
||||||
$paramMap[':key'] = $key;
|
$paramMap[':key'] = $key;
|
||||||
|
|
||||||
//For user specific preference, check if id matches as well
|
//For user specific preference, check if id matches as well
|
||||||
if ($isUserValue) {
|
if (isset($userId)) {
|
||||||
$auth = Zend_Auth::getInstance();
|
|
||||||
if ($auth->hasIdentity()) {
|
|
||||||
$id = $auth->getIdentity()->id;
|
|
||||||
|
|
||||||
$sql .= " AND subjid = :id";
|
$sql .= " AND subjid = :id";
|
||||||
$paramMap[':id'] = $id;
|
$paramMap[':id'] = $userId;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = Application_Common_Database::prepareAndExecute($sql, $paramMap, Application_Common_Database::COLUMN);
|
$result = Application_Common_Database::prepareAndExecute($sql, $paramMap, Application_Common_Database::COLUMN);
|
||||||
|
@ -140,14 +159,17 @@ class Application_Model_Preference
|
||||||
$paramMap[':key'] = $key;
|
$paramMap[':key'] = $key;
|
||||||
|
|
||||||
//For user specific preference, check if id matches as well
|
//For user specific preference, check if id matches as well
|
||||||
if ($isUserValue && $auth->hasIdentity()) {
|
if (isset($userId)) {
|
||||||
$sql .= " AND subjid = :id";
|
$sql .= " AND subjid = :id";
|
||||||
$paramMap[':id'] = $id;
|
$paramMap[':id'] = $userId;
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = Application_Common_Database::prepareAndExecute($sql, $paramMap, Application_Common_Database::COLUMN);
|
$result = Application_Common_Database::prepareAndExecute($sql, $paramMap, Application_Common_Database::COLUMN);
|
||||||
|
|
||||||
return ($result !== false) ? $result : "";
|
$res = ($result !== false) ? $result : "";
|
||||||
|
$cache->store($key, $res, $userId);
|
||||||
|
|
||||||
|
return $res;
|
||||||
}
|
}
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
header('HTTP/1.0 503 Service Unavailable');
|
header('HTTP/1.0 503 Service Unavailable');
|
||||||
|
|
|
@ -59,7 +59,7 @@ libtaglib-ocaml libao-ocaml libmad-ocaml ecasound \
|
||||||
libesd0 libportaudio2 libsamplerate0 rabbitmq-server patch \
|
libesd0 libportaudio2 libsamplerate0 rabbitmq-server patch \
|
||||||
php5-curl mpg123 monit python-virtualenv multitail libcamomile-ocaml-data \
|
php5-curl mpg123 monit python-virtualenv multitail libcamomile-ocaml-data \
|
||||||
libpulse0 vorbis-tools lsb-release lsof sudo mp3gain vorbisgain flac vorbis-tools \
|
libpulse0 vorbis-tools lsb-release lsof sudo mp3gain vorbisgain flac vorbis-tools \
|
||||||
pwgen libfaad2
|
pwgen libfaad2 php-apc
|
||||||
|
|
||||||
|
|
||||||
#install packages with --force-yes option (this is useful in the case
|
#install packages with --force-yes option (this is useful in the case
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue