Skip to content

Instantly share code, notes, and snippets.

@dsuch
Created Feb 3, 2015
Embed
What would you like to do?
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