Skip to content

Instantly share code, notes, and snippets.

@dsuch
Created February 3, 2015 19:17
Show Gist options
  • Save dsuch/3fe0b42d73ff2f9aed81 to your computer and use it in GitHub Desktop.
Save dsuch/3fe0b42d73ff2f9aed81 to your computer and use it in GitHub Desktop.
customer.get2
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
# stdlib
from random import choice
# Zato
from zato.server.service import Service
# A set of first and last names to generate customer names from
first = ('Richard', 'Victoria', 'Sebastian', 'James', 'Hannah')
last = ('Ogden', 'Piper', 'Edmunds', 'Murray', 'Young')
# Redis key we store our cache under
CACHE_KEY = 'example:customer:by-id'
class GetCustomer(Service):
name = 'customer.get2'
def get_customer(self, cust_id):
# Assume in an actual service this would look up the data in a real DB
return '{} {}'.format(choice(first), choice(last))
def handle(self):
self.log_input()
# Customer ID received on input
cust_id = self.request.payload['cust_id']
# Do we have their name in cache?
name = self.kvdb.conn.hget(CACHE_KEY, cust_id)
# If not, we need to ask the backend and cache the response
if not name:
name = self.get_customer(cust_id)
self.kvdb.conn.hset(CACHE_KEY, cust_id, name)
# Produce the response for the calling application.
self.response.payload = {'name': name}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment