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..6689f3e --- /dev/null +++ b/Cache/ApcAdapter.php @@ -0,0 +1,32 @@ +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) + { + $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 new file mode 100644 index 0000000..692495d --- /dev/null +++ b/Cache/SessionAdapter.php @@ -0,0 +1,32 @@ +setUrl($url); $this->setUsername($username); @@ -88,8 +97,61 @@ public function __construct($url, $username, $password, $productCode = null, $pr } } $this->_httpClient = $adapter; + + $this->detectCacheType(); + + + if($caching) + { + $this->turnOnCaching(); + } } - + + //TODO document + public function turnOnCaching() + { + $this->detectCacheType(); + if($this->_cache == false) + { + $this->_caching = false; + return $this->_caching; + } + $this->_caching = true; + return $this->_caching; + } + public function turnOffCaching() + { + $this->_caching = false; + return $this->_caching; + } + + public function detectCacheType() + { + if($this->_cache) + { + return $this->_cache; + } + + if(extension_loaded("memcache")) + { + $this->_cache = new CheddarGetter_Cache_MemcacheAdapter(); + } + else if(function_exists("apc_fetch")) + { + $this->_cache = new CheddarGetter_Cache_ApcAdapter(); + } + else if(isset($_SESSION)) + { + $this->_cache = new CheddarGetter_Cache_SessionAdapter(); + } + else + { + $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); + } + return $this->_cache; + } + /** * Set URL neccessary for for accessing the CheddarGetter API * @@ -250,8 +312,36 @@ 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->_cache->load($key); + //if there isn't a cached response, we're not going to use it + if(!$response) + $useCache = false; + } + //if we're not using the cached version, or if we're recaching + if(!$useCache || $recache) + { + //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->save($key, $response); + } + } + return $response; + } /** @@ -316,11 +406,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; } /** @@ -360,13 +460,40 @@ 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); - 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 = "customer_get_".$code;//."_".$id; + + //if caching is turned on and we're not refreshing + if($useCache && !$recache) + { + //get the cached response + $response = $this->_cache->load($key); + //if there isn't a cached response, we're not going to use it + if(!$response) + $useCache = false; + } + //if we're not using the cached version, or if we're recaching + if(!$useCache || $recache) + { + //get a new response + $response = new CheddarGetter_Response( + $this->request('/customers/get/' . (($id) ? 'id/'.$id : 'code/'.urlencode($code)) ) + ); + + //if caching is turned on + if($this->_caching) + { + //cache the response + $this->_cache->save($key, $response); + } + } + return $response; } - + /** * Get all customers * @@ -435,12 +562,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->save("customer_get_".$code, $response); + //$this->getCustomer($code, $id, true); + } + + return $response; } /** @@ -455,12 +594,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; } /** @@ -474,11 +621,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->_cache->remove("customer_get_".$code."_".$id); + } + + return $response; + } /** @@ -514,12 +670,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; } /** @@ -538,6 +702,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; } /** @@ -558,6 +730,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; } /** @@ -578,6 +758,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; } /** @@ -598,6 +786,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; } /** @@ -620,6 +816,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; } /** @@ -642,6 +846,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; } /** @@ -664,6 +876,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; } /** @@ -705,6 +925,8 @@ public function getPromotion($code, $id = null) { * @throws CheddarGetter_Client_Exception */ protected function request($path, array $args = null) { + + //echo "\nmade request\n"; $url = $this->_url . '/xml' . $path; if ($this->getProductId()) { $url .= '/productId/' . urlencode($this->getProductId());