From 1d04f529b4a09310aec4c278720c8adf65d9a808 Mon Sep 17 00:00:00 2001 From: Ben Serrette Date: Fri, 10 Aug 2012 15:49:14 +0000 Subject: [PATCH 1/9] Implemented some simple caching using session. --- Client.php | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 64 insertions(+), 3 deletions(-) diff --git a/Client.php b/Client.php index 6cdb732..b79a0b2 100644 --- a/Client.php +++ b/Client.php @@ -62,6 +62,12 @@ class CheddarGetter_Client { */ private $_httpClient; + /** + * @var Do we use caching? + */ + private $_caching = false; + + /** * Constructor * @@ -89,6 +95,37 @@ public function __construct($url, $username, $password, $productCode = null, $pr } $this->_httpClient = $adapter; } + + //TODO document + public function turnOnCaching() + { + $this->_caching = true; + } + public function turnOffCaching() + { + $this->_caching = false; + } + public function cache($key, $value) + { + //TODO detect type of caching (memcache, session, etc) + if(!isset($_SESSION["CGCaching"])) + $_SESSION["CGCaching"] = array(); + + $_SESSION["CGCaching"][$key] = $value; + + } + public function getCached($key) + { + $value = false; + if(isset($_SESSION["CGCaching"][$key])) + $value = $_SESSION["CGCaching"][$key]; + + return $value; + } + public function emptyCache() + { + unset( $_SESSION["CGCaching"] ); + } /** * Set URL neccessary for for accessing the CheddarGetter API @@ -362,9 +399,31 @@ public function getCustomersList(array $filters = null) { */ public function getCustomer($code, $id = null) { $this->_requireIdentifier($code, $id); - return new CheddarGetter_Response( - $this->request('/customers/get/' . (($id) ? 'id/'.$id : 'code/'.urlencode($code)) ) - ); + + $response = null; + $useCache = $this->_caching; //do we want to used a cached version? + $key = false; + + if($useCache) + { + $key = "customer_get_".$id."_".$code; + $response = $this->getCached($key); + if(!$response) + $useCache = false; + } + + if(!$useCache) + { + $response = new CheddarGetter_Response( + $this->request('/customers/get/' . (($id) ? 'id/'.$id : 'code/'.urlencode($code)) ) + ); + + //if key is not false, we've tried to use cached version, but it didn't exist yet + if($key) + $this->cache($key, $response); + } + + return $response; } /** @@ -705,6 +764,8 @@ public function getPromotion($code, $id = null) { * @throws CheddarGetter_Client_Exception */ protected function request($path, array $args = null) { + +echo "made request\n"; $url = $this->_url . '/xml' . $path; if ($this->getProductId()) { $url .= '/productId/' . urlencode($this->getProductId()); From 8a9d94028dbe14fd91eb773b174f8d72596191e3 Mon Sep 17 00:00:00 2001 From: Ben Serrette Date: Tue, 14 Aug 2012 19:10:59 +0000 Subject: [PATCH 2/9] using editCustomer, etc will automatically update the cache for a given user. --- Client.php | 141 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 125 insertions(+), 16 deletions(-) diff --git a/Client.php b/Client.php index b79a0b2..04a411a 100644 --- a/Client.php +++ b/Client.php @@ -113,9 +113,17 @@ public function cache($key, $value) $_SESSION["CGCaching"][$key] = $value; + } + public function decache($key) + { + //TODO detect type of caching (memcache, session, etc) + if(!isset($_SESSION["CGCaching"][$key])) + unset($_SESSION["CGCaching"][$key]); + } public function getCached($key) { + echo "\nget cached\n"; $value = false; if(isset($_SESSION["CGCaching"][$key])) $value = $_SESSION["CGCaching"][$key]; @@ -397,35 +405,43 @@ public function getCustomersList(array $filters = null) { * @return CheddarGetter_Response * @throws CheddarGetter_Response_Exception */ - public function getCustomer($code, $id = null) { + public function getCustomer($code, $id = null, $recache = false) { $this->_requireIdentifier($code, $id); $response = null; $useCache = $this->_caching; //do we want to used a cached version? - $key = false; + $key = "customer_get_".$code;//."_".$id; - if($useCache) + //if caching is turned on and we're not refreshing + if($useCache && !$recache) { - $key = "customer_get_".$id."_".$code; + //get the cached response $response = $this->getCached($key); + //if there isn't a cached response, we're not going to use it if(!$response) $useCache = false; + // echo $key; } - - if(!$useCache) + //print_r($key); + //if we're not using the cached version, or if we're recaching + if(!$useCache || $recache) { + //echo $key; + //get a new response $response = new CheddarGetter_Response( $this->request('/customers/get/' . (($id) ? 'id/'.$id : 'code/'.urlencode($code)) ) ); - //if key is not false, we've tried to use cached version, but it didn't exist yet - if($key) + //if caching is turned on + if($this->_caching) + { + //cache the response $this->cache($key, $response); + } } - return $response; } - + /** * Get all customers * @@ -494,12 +510,24 @@ public function newCustomer(array $data) { */ public function editCustomer($code, $id = null, array $data) { $this->_requireIdentifier($code, $id); - return new CheddarGetter_Response( + + $response = new CheddarGetter_Response( $this->request( '/customers/edit/' . (($id) ? 'id/'.$id : 'code/'.urlencode($code)), $data ) ); + + + //if caching is turned on + if($this->_caching) + { + //recache the customer that we just edited + $this->cache("customer_get_".$code, $response); + //$this->getCustomer($code, $id, true); + } + + return $response; } /** @@ -514,12 +542,20 @@ public function editCustomer($code, $id = null, array $data) { */ public function editCustomerOnly($code, $id = null, array $data) { $this->_requireIdentifier($code, $id); - return new CheddarGetter_Response( + $response = new CheddarGetter_Response( $this->request( '/customers/edit-customer/' . (($id) ? 'id/'.$id : 'code/'.urlencode($code)), $data ) ); + + //if we use caching, we'll have to re-get the customer after every edit + if($this->_caching) + { + $this->getCustomer($code, $id, true); + } + + return $response; } /** @@ -533,11 +569,20 @@ public function editCustomerOnly($code, $id = null, array $data) { */ public function deleteCustomer($code, $id = null) { $this->_requireIdentifier($code, $id); - return new CheddarGetter_Response( + $response = new CheddarGetter_Response( $this->request( '/customers/delete/' . (($id) ? 'id/'.$id : 'code/'.urlencode($code)) ) - ); + ); + + //if we use caching, we'll have to uncache this customer after every edit + if($this->_caching) + { + $this->decache("customer_get_".$code."_".$id); + } + + return $response; + } /** @@ -573,12 +618,20 @@ public function deleteCustomers() { */ public function editSubscription($code, $id = null, array $data) { $this->_requireIdentifier($code, $id); - return new CheddarGetter_Response( + $response = new CheddarGetter_Response( $this->request( '/customers/edit-subscription/' . (($id) ? 'id/'.$id : 'code/'.urlencode($code)), $data ) ); + + //if we use caching, we'll have to re-get the customer after every edit + if($this->_caching) + { + $this->getCustomer($code, $id, true); + } + + return $response; } /** @@ -597,6 +650,14 @@ public function cancelSubscription($code, $id = null) { '/customers/cancel/' . (($id) ? 'id/'.$id : 'code/'.urlencode($code)) ) ); + + //if we use caching, we'll have to re-get the customer after every edit + if($this->_caching) + { + $this->getCustomer($code, $id, true); + } + + return $response; } /** @@ -617,6 +678,14 @@ public function addItemQuantity($code, $id = null, array $data) { $data ) ); + + //if we use caching, we'll have to re-get the customer after every edit + if($this->_caching) + { + $this->getCustomer($code, $id, true); + } + + return $response; } /** @@ -637,6 +706,14 @@ public function removeItemQuantity($code, $id = null, array $data) { $data ) ); + + //if we use caching, we'll have to re-get the customer after every edit + if($this->_caching) + { + $this->getCustomer($code, $id, true); + } + + return $response; } /** @@ -657,6 +734,14 @@ public function setItemQuantity($code, $id = null, array $data) { $data ) ); + + //if we use caching, we'll have to re-get the customer after every edit + if($this->_caching) + { + $this->getCustomer($code, $id, true); + } + + return $response; } /** @@ -679,6 +764,14 @@ public function addCharge($code, $id = null, array $data) { $data ) ); + + //if we use caching, we'll have to re-get the customer after every edit + if($this->_caching) + { + $this->getCustomer($code, $id, true); + } + + return $response; } /** @@ -701,6 +794,14 @@ public function deleteCharge($code, $id = null, array $data) { $data ) ); + + //if we use caching, we'll have to re-get the customer after every edit + if($this->_caching) + { + $this->getCustomer($code, $id, true); + } + + return $response; } /** @@ -723,6 +824,14 @@ public function newOneTimeInvoice($code, $id = null, array $data) { $data ) ); + + //if we use caching, we'll have to re-get the customer after every edit + if($this->_caching) + { + $this->getCustomer($code, $id, true); + } + + return $response; } /** @@ -765,7 +874,7 @@ public function getPromotion($code, $id = null) { */ protected function request($path, array $args = null) { -echo "made request\n"; + echo "made request\n"; $url = $this->_url . '/xml' . $path; if ($this->getProductId()) { $url .= '/productId/' . urlencode($this->getProductId()); From 2bf527dae626c2e27123e767cb7375b12851bb63 Mon Sep 17 00:00:00 2001 From: Ben Serrette Date: Thu, 16 Aug 2012 19:39:13 +0000 Subject: [PATCH 3/9] Checks to see if sessions are disabled. --- Client.php | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/Client.php b/Client.php index 04a411a..943a83b 100644 --- a/Client.php +++ b/Client.php @@ -67,6 +67,11 @@ class CheddarGetter_Client { */ private $_caching = false; + /** + * What kind of caching should we use? + */ + private $_cacheType = false; + /** * Constructor @@ -99,11 +104,20 @@ public function __construct($url, $username, $password, $productCode = null, $pr //TODO document public function turnOnCaching() { + $this->detectCacheType(); + if($this->_cacheType == false) + { + throw new CheddarGetter_Client_Exception("Memcache, APC, and Sessions are unavailable on this system and caching cannot be turned on.", CheddarGetter_Client_Exception::UNKNOWN); + $this->_caching = false; + return $this->_caching; + } $this->_caching = true; + return $this->_caching; } public function turnOffCaching() { $this->_caching = false; + return $this->_caching; } public function cache($key, $value) { @@ -121,9 +135,21 @@ public function decache($key) unset($_SESSION["CGCaching"][$key]); } + public function detectCacheType() + { + if(isset($_SESSION)) + { + $this->_cacheType = "session"; + } + else + { + $this->_cacheType = false; + } + return $this->_cacheType; + } public function getCached($key) { - echo "\nget cached\n"; + //echo "\nget cached\n"; $value = false; if(isset($_SESSION["CGCaching"][$key])) $value = $_SESSION["CGCaching"][$key]; @@ -874,7 +900,7 @@ public function getPromotion($code, $id = null) { */ protected function request($path, array $args = null) { - echo "made request\n"; + // echo "made request\n"; $url = $this->_url . '/xml' . $path; if ($this->getProductId()) { $url .= '/productId/' . urlencode($this->getProductId()); From f200eb6c14adc428e6b4a1f3784e70c0ff39d574 Mon Sep 17 00:00:00 2001 From: Ben Serrette Date: Fri, 17 Aug 2012 16:43:38 +0000 Subject: [PATCH 4/9] If available, uses APC caching. --- Client.php | 64 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 12 deletions(-) diff --git a/Client.php b/Client.php index 943a83b..828b916 100644 --- a/Client.php +++ b/Client.php @@ -122,22 +122,50 @@ public function turnOffCaching() public function cache($key, $value) { //TODO detect type of caching (memcache, session, etc) - if(!isset($_SESSION["CGCaching"])) - $_SESSION["CGCaching"] = array(); + switch($this->_cacheType) + { + case "session" : + if(!isset($_SESSION["CGCaching"])) + $_SESSION["CGCaching"] = array(); + $_SESSION["CGCaching"][$key] = $value; + break; + case "APC" : + $key = "CGCaching_".$key; + apc_store($key, $value); + + break; + case "memcache" : + break; + default: + return false; + break; + } - $_SESSION["CGCaching"][$key] = $value; } public function decache($key) { - //TODO detect type of caching (memcache, session, etc) - if(!isset($_SESSION["CGCaching"][$key])) - unset($_SESSION["CGCaching"][$key]); - + switch($this->_cacheType) + { + case "session" : + //TODO detect type of caching (memcache, session, etc) + if(!isset($_SESSION["CGCaching"][$key])) + unset($_SESSION["CGCaching"][$key]); + break; + case "APC" : + $key = "CGCaching_".$key; + apc_delete($key); + break; + + } } public function detectCacheType() { - if(isset($_SESSION)) + if(function_exists("apc_fetch")) + { + $this->_cacheType = "APC"; + } + else if(isset($_SESSION)) { $this->_cacheType = "session"; } @@ -145,15 +173,27 @@ public function detectCacheType() { $this->_cacheType = false; } + echo "\nUsing ".$this->_cacheType."\n"; return $this->_cacheType; } public function getCached($key) { //echo "\nget cached\n"; $value = false; - if(isset($_SESSION["CGCaching"][$key])) - $value = $_SESSION["CGCaching"][$key]; - + switch($this->_cacheType) + { + case "session" : + if(isset($_SESSION["CGCaching"][$key])) + $value = $_SESSION["CGCaching"][$key]; + break; + case "APC" : + $key = "CGCaching_".$key; + if(apc_fetch($key)) + $value = apc_fetch($key); + break; + + } + echo "\nGot Cached\n"; return $value; } public function emptyCache() @@ -900,7 +940,7 @@ public function getPromotion($code, $id = null) { */ protected function request($path, array $args = null) { - // echo "made request\n"; + echo "\nmade request\n"; $url = $this->_url . '/xml' . $path; if ($this->getProductId()) { $url .= '/productId/' . urlencode($this->getProductId()); From cd16d10ecadd7a2da0fb5f015740c168b042e7c4 Mon Sep 17 00:00:00 2001 From: Ben Serrette Date: Fri, 17 Aug 2012 18:56:30 +0000 Subject: [PATCH 5/9] Uses memcache if available. Cannot clear the memcache cache. --- Client.php | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/Client.php b/Client.php index 828b916..db826fa 100644 --- a/Client.php +++ b/Client.php @@ -72,6 +72,11 @@ class CheddarGetter_Client { */ private $_cacheType = false; + /** + * If $_cacheType is memcache, then the link to the server will be here. + */ + public $_mcs = false; + /** * Constructor @@ -135,6 +140,8 @@ public function cache($key, $value) break; case "memcache" : + $key = "CGCaching_".$key; + $this->_mcs->set($key, $value); break; default: return false; @@ -156,12 +163,23 @@ public function decache($key) $key = "CGCaching_".$key; apc_delete($key); break; + case "memcache" : + $key = "CGCaching_".$key; + $this->_mcs->delete($key); + break; + default: return false; } } public function detectCacheType() { - if(function_exists("apc_fetch")) + if(extension_loaded("memcache")) + { + $this->_cacheType = "memcache"; + $this->_mcs = new Memcache; + $this->_mcs->connect("localhost"); + } + else if(function_exists("apc_fetch")) { $this->_cacheType = "APC"; } @@ -191,6 +209,12 @@ public function getCached($key) if(apc_fetch($key)) $value = apc_fetch($key); break; + case "memcache" : + $key = "CGCaching_".$key; + if($this->_mcs->get($key)) + $value = $this->_mcs->get($key); + break; + default: return false; } echo "\nGot Cached\n"; @@ -198,7 +222,23 @@ public function getCached($key) } public function emptyCache() { - unset( $_SESSION["CGCaching"] ); + switch($this->_cacheType) + { + case "session" : + unset( $_SESSION["CGCaching"] ); + break; + case "APC" : + $cache = apc_cache_info("user"); + $cachelist = $cache["cache_list"]; + foreach($cachelist as $key => $val) + { + if(substr($val["info"], 0, 9) == "CGCaching") + { + apc_delete($val["info"]); + } + } + break; + } } /** From fd077c55ead5224014c2d2fc3bffc0441c14c177 Mon Sep 17 00:00:00 2001 From: Ben Serrette Date: Fri, 17 Aug 2012 20:05:11 +0000 Subject: [PATCH 6/9] Plans are cached. Can now turn on caching in constructor. --- Client.php | 62 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 54 insertions(+), 8 deletions(-) diff --git a/Client.php b/Client.php index db826fa..edb0c92 100644 --- a/Client.php +++ b/Client.php @@ -88,7 +88,7 @@ class CheddarGetter_Client { * @param string $productId * @param CheddarGetter_Client_AdapterInterface $adapter */ - public function __construct($url, $username, $password, $productCode = null, $productId = null, CheddarGetter_Client_AdapterInterface $adapter = null) { + public function __construct($url, $username, $password, $productCode = null, $productId = null, CheddarGetter_Client_AdapterInterface $adapter = null, $caching = false) { $this->setUrl($url); $this->setUsername($username); @@ -104,6 +104,11 @@ public function __construct($url, $username, $password, $productCode = null, $pr } } $this->_httpClient = $adapter; + + if($caching) + { + $this->turnOnCaching(); + } } //TODO document @@ -191,7 +196,7 @@ public function detectCacheType() { $this->_cacheType = false; } - echo "\nUsing ".$this->_cacheType."\n"; + //echo "\nUsing ".$this->_cacheType."\n"; return $this->_cacheType; } public function getCached($key) @@ -217,7 +222,7 @@ public function getCached($key) default: return false; } - echo "\nGot Cached\n"; + //echo "\nGot Cached\n"; return $value; } public function emptyCache() @@ -401,8 +406,39 @@ public function __call($method, $args) { * @return CheddarGetter_Response * @throws CheddarGetter_Response_Exception */ - public function getPlans(array $filters = null) { - return new CheddarGetter_Response( $this->request('/plans/get', $filters) ); + public function getPlans(array $filters = null, $recache = false) { + + $response = null; + $useCache = $this->_caching; //do we want to used a cached version? + $key = "allPlans";//."_".$id; + + //if caching is turned on and we're not refreshing + if($useCache && !$recache) + { + //get the cached response + $response = $this->getCached($key); + //if there isn't a cached response, we're not going to use it + if(!$response) + $useCache = false; + // echo $key; + } + //print_r($key); + //if we're not using the cached version, or if we're recaching + if(!$useCache || $recache) + { + //echo $key; + //get a new response + $response = new CheddarGetter_Response( $this->request('/plans/get', $filters) ); + + //if caching is turned on + if($this->_caching) + { + //cache the response + $this->cache($key, $response); + } + } + return $response; + } /** @@ -467,11 +503,21 @@ public function editPlan($code, $id = null, array $data) { */ public function deletePlan($code, $id = null) { $this->_requireIdentifier($code, $id); - return new CheddarGetter_Response( + $response = new CheddarGetter_Response( $this->request( '/plans/delete/' . (($id) ? 'id/'.$id : 'code/'.urlencode($code)) ) ); + + //if caching is turned on + if($this->_caching) + { + //recache the customer that we just edited + $this->getPlans(null, true); + //$this->getCustomer($code, $id, true); + } + + return $response; } /** @@ -526,7 +572,7 @@ public function getCustomer($code, $id = null, $recache = false) { //if there isn't a cached response, we're not going to use it if(!$response) $useCache = false; - // echo $key; + //echo $key; } //print_r($key); //if we're not using the cached version, or if we're recaching @@ -980,7 +1026,7 @@ public function getPromotion($code, $id = null) { */ protected function request($path, array $args = null) { - echo "\nmade request\n"; + //echo "\nmade request\n"; $url = $this->_url . '/xml' . $path; if ($this->getProductId()) { $url .= '/productId/' . urlencode($this->getProductId()); From 263f398109351f96c0f41525f7eb3f7c6de34c23 Mon Sep 17 00:00:00 2001 From: Ben Serrette Date: Wed, 29 Aug 2012 20:29:45 +0000 Subject: [PATCH 7/9] Changing to adapter pattern. Session adapter created. --- Client.php | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/Client.php b/Client.php index edb0c92..c8882d3 100644 --- a/Client.php +++ b/Client.php @@ -67,15 +67,8 @@ class CheddarGetter_Client { */ private $_caching = false; - /** - * What kind of caching should we use? - */ - private $_cacheType = false; - - /** - * If $_cacheType is memcache, then the link to the server will be here. - */ - public $_mcs = false; + /** @var What kind of caching do we use? */ + private $_cache; /** @@ -105,6 +98,28 @@ public function __construct($url, $username, $password, $productCode = null, $pr } $this->_httpClient = $adapter; + /* if(extension_loaded("memcache")) + { + $this->_cacheType = "memcache"; + $this->_mcs = new Memcache; + $this->_mcs->connect("localhost"); + } + else if(function_exists("apc_fetch")) + { + $this->_cacheType = "APC"; + } +else */ + if(isset($_SESSION)) + { + $this->_cache = new CheddarGetter_Cache_SessionAdapter(); + } + else + { + $this->_cache = false; + //issue warning + } + + if($caching) { $this->turnOnCaching(); @@ -129,7 +144,7 @@ public function turnOffCaching() $this->_caching = false; return $this->_caching; } - public function cache($key, $value) + /*public function cache($key, $value) { //TODO detect type of caching (memcache, session, etc) switch($this->_cacheType) @@ -175,7 +190,7 @@ public function decache($key) default: return false; } - } +}*/ public function detectCacheType() { if(extension_loaded("memcache")) @@ -199,6 +214,7 @@ public function detectCacheType() //echo "\nUsing ".$this->_cacheType."\n"; return $this->_cacheType; } + /* public function getCached($key) { //echo "\nget cached\n"; @@ -244,7 +260,7 @@ public function emptyCache() } break; } - } +}*/ /** * Set URL neccessary for for accessing the CheddarGetter API From 04d6bebcb14b143e76e86313e573304075e3bb87 Mon Sep 17 00:00:00 2001 From: Ben Serrette Date: Wed, 29 Aug 2012 21:26:05 +0000 Subject: [PATCH 8/9] Updated caching to use adapter pattern for session, apc, and memcache. --- Cache/AdapterInterface.php | 22 ++++++++++++++ Cache/ApcAdapter.php | 26 +++++++++++++++++ Cache/MemcacheAdapter.php | 32 ++++++++++++++++++++ Cache/SessionAdapter.php | 28 ++++++++++++++++++ Client.php | 60 ++++++++++++-------------------------- 5 files changed, 126 insertions(+), 42 deletions(-) create mode 100644 Cache/AdapterInterface.php create mode 100644 Cache/ApcAdapter.php create mode 100644 Cache/MemcacheAdapter.php create mode 100644 Cache/SessionAdapter.php diff --git a/Cache/AdapterInterface.php b/Cache/AdapterInterface.php new file mode 100644 index 0000000..cd81d8a --- /dev/null +++ b/Cache/AdapterInterface.php @@ -0,0 +1,22 @@ + + */ +/** + * Adapter interface for caching responses + * @category CheddarGetter + * @package CheddarGetter + * @author Ben Serrette + */ + +interface CheddarGetter_Cache_AdapterInterface { + + function __construct(); + function load($key); + function save($key, $value); + function remove($key); + +} diff --git a/Cache/ApcAdapter.php b/Cache/ApcAdapter.php new file mode 100644 index 0000000..4aa5e22 --- /dev/null +++ b/Cache/ApcAdapter.php @@ -0,0 +1,26 @@ +cache = new Memcache; + $this->cache->connect("localhost"); + }; + + public function save($key, $value) + { + $key = "CGCaching_".$key; + $this->cache->set($key, $value); + } + public function remove($key) + { + $key = "CGCaching_".$key; + $this->cache->delete($key); + } + public function load($key) + { + //echo "\nget cached\n"; + $value = false; + $key = "CGCaching_".$key; + if($this->cache->get($key)) + $value = $this->cache->get($key); + return $value; + } +} + diff --git a/Cache/SessionAdapter.php b/Cache/SessionAdapter.php new file mode 100644 index 0000000..dd0a371 --- /dev/null +++ b/Cache/SessionAdapter.php @@ -0,0 +1,28 @@ +_httpClient = $adapter; - /* if(extension_loaded("memcache")) - { - $this->_cacheType = "memcache"; - $this->_mcs = new Memcache; - $this->_mcs->connect("localhost"); - } - else if(function_exists("apc_fetch")) - { - $this->_cacheType = "APC"; - } -else */ - if(isset($_SESSION)) - { - $this->_cache = new CheddarGetter_Cache_SessionAdapter(); - } - else - { - $this->_cache = false; - //issue warning - } + $this->detectCacheType(); if($caching) @@ -130,7 +111,7 @@ public function __construct($url, $username, $password, $productCode = null, $pr public function turnOnCaching() { $this->detectCacheType(); - if($this->_cacheType == false) + if($this->_cache == false) { throw new CheddarGetter_Client_Exception("Memcache, APC, and Sessions are unavailable on this system and caching cannot be turned on.", CheddarGetter_Client_Exception::UNKNOWN); $this->_caching = false; @@ -195,24 +176,25 @@ public function detectCacheType() { if(extension_loaded("memcache")) { - $this->_cacheType = "memcache"; - $this->_mcs = new Memcache; - $this->_mcs->connect("localhost"); + //$this->_cacheType = "memcache"; + //$this->_mcs = new Memcache; + //$this->_mcs->connect("localhost"); + $this->_cache = new CheddarGetter_Cache_MemcacheAdapter(); } else if(function_exists("apc_fetch")) { - $this->_cacheType = "APC"; + $$this->_cache = new CheddarGetter_Cache_ApcAdapter(); } else if(isset($_SESSION)) { - $this->_cacheType = "session"; + $this->_cache = new CheddarGetter_Cache_SessionAdapter(); } else { - $this->_cacheType = false; - } - //echo "\nUsing ".$this->_cacheType."\n"; - return $this->_cacheType; + $this->_cache = false; + //issue warning + } + return $this->_cache; } /* public function getCached($key) @@ -432,17 +414,14 @@ public function getPlans(array $filters = null, $recache = false) { if($useCache && !$recache) { //get the cached response - $response = $this->getCached($key); + $response = $this->_cache->load($key); //if there isn't a cached response, we're not going to use it if(!$response) $useCache = false; - // echo $key; } - //print_r($key); //if we're not using the cached version, or if we're recaching if(!$useCache || $recache) { - //echo $key; //get a new response $response = new CheddarGetter_Response( $this->request('/plans/get', $filters) ); @@ -450,7 +429,7 @@ public function getPlans(array $filters = null, $recache = false) { if($this->_caching) { //cache the response - $this->cache($key, $response); + $this->_cache->save($key, $response); } } return $response; @@ -584,17 +563,14 @@ public function getCustomer($code, $id = null, $recache = false) { if($useCache && !$recache) { //get the cached response - $response = $this->getCached($key); + $response = $this->_cache->load($key); //if there isn't a cached response, we're not going to use it if(!$response) $useCache = false; - //echo $key; } - //print_r($key); //if we're not using the cached version, or if we're recaching if(!$useCache || $recache) { - //echo $key; //get a new response $response = new CheddarGetter_Response( $this->request('/customers/get/' . (($id) ? 'id/'.$id : 'code/'.urlencode($code)) ) @@ -604,7 +580,7 @@ public function getCustomer($code, $id = null, $recache = false) { if($this->_caching) { //cache the response - $this->cache($key, $response); + $this->_cache->save($key, $response); } } return $response; @@ -691,7 +667,7 @@ public function editCustomer($code, $id = null, array $data) { if($this->_caching) { //recache the customer that we just edited - $this->cache("customer_get_".$code, $response); + $this->_cache->save("customer_get_".$code, $response); //$this->getCustomer($code, $id, true); } @@ -746,7 +722,7 @@ public function deleteCustomer($code, $id = null) { //if we use caching, we'll have to uncache this customer after every edit if($this->_caching) { - $this->decache("customer_get_".$code."_".$id); + $this->_cache->remove("customer_get_".$code."_".$id); } return $response; From eb1319df4bd81ee00148186ed5207a0313aba35d Mon Sep 17 00:00:00 2001 From: Ben Serrette Date: Thu, 30 Aug 2012 15:21:51 +0000 Subject: [PATCH 9/9] Fixed errors in adapters and cleaned up some code. --- Cache/ApcAdapter.php | 10 +++- Cache/MemcacheAdapter.php | 7 ++- Cache/SessionAdapter.php | 8 ++- Client.php | 106 +++----------------------------------- 4 files changed, 26 insertions(+), 105 deletions(-) diff --git a/Cache/ApcAdapter.php b/Cache/ApcAdapter.php index 4aa5e22..6689f3e 100644 --- a/Cache/ApcAdapter.php +++ b/Cache/ApcAdapter.php @@ -2,24 +2,30 @@ class CheddarGetter_Cache_ApcAdapter implements CheddarGetter_Cache_AdapterInterface { - public function __construct(){}; + public function __construct(){ + // echo "Making APC \n"; + } public function save($key, $value) { $key = "CGCaching_".$key; apc_store($key, $value); + // echo "Saving \n"; } public function remove($key) { $key = "CGCaching_".$key; apc_delete($key); + // echo "Removing \n"; } public function load($key) { - //echo "\nget cached\n"; + + $key = "CGCaching_".$key; $value = false; if(apc_fetch($key)) $value = apc_fetch($key); + // echo "Loading \n"; return $value; } } diff --git a/Cache/MemcacheAdapter.php b/Cache/MemcacheAdapter.php index 8fe3485..b9ce957 100644 --- a/Cache/MemcacheAdapter.php +++ b/Cache/MemcacheAdapter.php @@ -7,25 +7,28 @@ public function __construct() { $this->cache = new Memcache; $this->cache->connect("localhost"); - }; + //echo "making memcache\n"; + } public function save($key, $value) { $key = "CGCaching_".$key; $this->cache->set($key, $value); + //echo "Saving \n"; } public function remove($key) { $key = "CGCaching_".$key; $this->cache->delete($key); + //echo "Removing\n"; } public function load($key) { - //echo "\nget cached\n"; $value = false; $key = "CGCaching_".$key; if($this->cache->get($key)) $value = $this->cache->get($key); + //echo "Loading \n"; return $value; } } diff --git a/Cache/SessionAdapter.php b/Cache/SessionAdapter.php index dd0a371..692495d 100644 --- a/Cache/SessionAdapter.php +++ b/Cache/SessionAdapter.php @@ -2,25 +2,29 @@ class CheddarGetter_Cache_SessionAdapter implements CheddarGetter_Cache_AdapterInterface { - public function __construct(){}; + public function __construct(){ + // echo "making Session \n"; + } public function save($key, $value) { if(!isset($_SESSION["CGCaching"])) $_SESSION["CGCaching"] = array(); $_SESSION["CGCaching"][$key] = $value; + // echo "Saving \n"; } public function remove($key) { if(!isset($_SESSION["CGCaching"][$key])) unset($_SESSION["CGCaching"][$key]); + // echo "Removing \n"; } public function load($key) { - //echo "\nget cached\n"; $value = false; if(isset($_SESSION["CGCaching"][$key])) $value = $_SESSION["CGCaching"][$key]; + // echo "Loading \n"; return $value; } } diff --git a/Client.php b/Client.php index 1d25d5d..e8213e6 100644 --- a/Client.php +++ b/Client.php @@ -113,7 +113,6 @@ public function turnOnCaching() $this->detectCacheType(); if($this->_cache == false) { - throw new CheddarGetter_Client_Exception("Memcache, APC, and Sessions are unavailable on this system and caching cannot be turned on.", CheddarGetter_Client_Exception::UNKNOWN); $this->_caching = false; return $this->_caching; } @@ -125,65 +124,21 @@ public function turnOffCaching() $this->_caching = false; return $this->_caching; } - /*public function cache($key, $value) + + public function detectCacheType() { - //TODO detect type of caching (memcache, session, etc) - switch($this->_cacheType) + if($this->_cache) { - case "session" : - if(!isset($_SESSION["CGCaching"])) - $_SESSION["CGCaching"] = array(); - $_SESSION["CGCaching"][$key] = $value; - break; - case "APC" : - $key = "CGCaching_".$key; - apc_store($key, $value); - - break; - case "memcache" : - $key = "CGCaching_".$key; - $this->_mcs->set($key, $value); - break; - default: - return false; - break; + return $this->_cache; } - - } - public function decache($key) - { - switch($this->_cacheType) - { - case "session" : - //TODO detect type of caching (memcache, session, etc) - if(!isset($_SESSION["CGCaching"][$key])) - unset($_SESSION["CGCaching"][$key]); - break; - case "APC" : - $key = "CGCaching_".$key; - apc_delete($key); - break; - case "memcache" : - $key = "CGCaching_".$key; - $this->_mcs->delete($key); - break; - default: return false; - - } -}*/ - public function detectCacheType() - { if(extension_loaded("memcache")) { - //$this->_cacheType = "memcache"; - //$this->_mcs = new Memcache; - //$this->_mcs->connect("localhost"); $this->_cache = new CheddarGetter_Cache_MemcacheAdapter(); } else if(function_exists("apc_fetch")) { - $$this->_cache = new CheddarGetter_Cache_ApcAdapter(); + $this->_cache = new CheddarGetter_Cache_ApcAdapter(); } else if(isset($_SESSION)) { @@ -192,58 +147,11 @@ public function detectCacheType() else { $this->_cache = false; - //issue warning + throw new CheddarGetter_Client_Exception("Memcache, APC, and Sessions are unavailable on this system and caching cannot be turned on.", CheddarGetter_Client_Exception::UNKNOWN); } return $this->_cache; } - /* - public function getCached($key) - { - //echo "\nget cached\n"; - $value = false; - switch($this->_cacheType) - { - case "session" : - if(isset($_SESSION["CGCaching"][$key])) - $value = $_SESSION["CGCaching"][$key]; - break; - case "APC" : - $key = "CGCaching_".$key; - if(apc_fetch($key)) - $value = apc_fetch($key); - break; - case "memcache" : - $key = "CGCaching_".$key; - if($this->_mcs->get($key)) - $value = $this->_mcs->get($key); - break; - default: return false; - - } - //echo "\nGot Cached\n"; - return $value; - } - public function emptyCache() - { - switch($this->_cacheType) - { - case "session" : - unset( $_SESSION["CGCaching"] ); - break; - case "APC" : - $cache = apc_cache_info("user"); - $cachelist = $cache["cache_list"]; - foreach($cachelist as $key => $val) - { - if(substr($val["info"], 0, 9) == "CGCaching") - { - apc_delete($val["info"]); - } - } - break; - } -}*/ - + /** * Set URL neccessary for for accessing the CheddarGetter API *