Skip to content

Instantly share code, notes, and snippets.

@dsuch
Created February 3, 2015 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsuch/2211f5960d68c03e4621 to your computer and use it in GitHub Desktop.
Save dsuch/2211f5960d68c03e4621 to your computer and use it in GitHub Desktop.
customer.get1
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
# stdlib
from json import dumps
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.get1'
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 = dumps({'name': name})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment