Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Attempt to integrate basic django caching w/ pycheddar

  • Loading branch information...
commit 8b4283b6c19fcc2542abae88bd49d43b35a3f714 1 parent 9a20078
Ryan Showalter authored
Showing with 15 additions and 3 deletions.
  1. +15 −3 pycheddar/__init__.py
View
18 pycheddar/__init__.py
@@ -8,6 +8,8 @@
from xml.etree import ElementTree
from urllib import urlencode
+from django.core.cache import cache
+
VERSION = '0.9.3'
class CheddarGetter:
@@ -496,9 +498,17 @@ def get(cls, code):
Raises NotFound if the customer code does not exist
in CheddarGetter."""
- xml = CheddarGetter.request('/customers/get/', code = code)
- for customer_xml in xml.getiterator(tag='customer'):
- return Customer.from_xml(customer_xml)
+ from django.conf import settings
+ cache_string = 'cg_{0}_customer_{1}'.format(settings.CHEDDARGETTER_PRODUCT_CODE, code)
+ cached_customer = cache.get(cache_string)
+ if cached_customer is not None:
+ return pickle.loads(cached_customer)
+ else:
+ xml = CheddarGetter.request('/customers/get/', code=code)
+ for customer_xml in xml.getiterator(tag='customer'):
+ customer = Customer.from_xml(customer_xml)
+ cache.set(cache_string, pickle.dumps(customer), 3600)
+ return customer
def validate(self):
@@ -557,8 +567,10 @@ def save(self):
# either way, I should get a well-formed customer XML response
# that can now be loaded into this object
+ from django.conf import settings
for customer_xml in xml.getiterator(tag='customer'):
self._load_data_from_xml(customer_xml)
+ cache.set('cg_{0}_customer_{1}'.format(settings.CHEDDARGETTER_PRODUCT_CODE, code), pickle.dumps(Customer.from_xml(customer_xml)), 3600)
break
return self
Please sign in to comment.
Something went wrong with that request. Please try again.