Every service has access to a built-in cache via self.cache. The cache supports get, set, delete, and exists, with optional expiry in seconds on set. Values are serialized to JSON automatically, so strings, integers, floats, dicts, lists, booleans and None are all supported out of the box.
# -*- coding: utf-8 -*-
# Zato
from zato.server.service import Service
class MyService(Service):
def handle(self):
# Store a value
self.cache.set('my-key', 'my-value')
# Read it back
value = self.cache.get('my-key')
self.logger.info(value)
# -*- coding: utf-8 -*-
# Zato
from zato.server.service import Service
class MyService(Service):
def handle(self):
# Store a value that expires in 60 seconds
self.cache.set('session-token', 'abc123', expiry=60)
# Read it back - returns the value if not expired, None otherwise
value = self.cache.get('session-token')
self.logger.info(value)
# -*- coding: utf-8 -*-
# Zato
from zato.server.service import Service
class MyService(Service):
def handle(self):
user_data = {
'username': 'joe',
'email': 'joe@example.com',
'roles': ['admin', 'editor'],
}
# Store a dict with a 5-minute expiry
self.cache.set('user:123', user_data, expiry=300)
# Read it back as a Python dict
cached = self.cache.get('user:123')
self.logger.info(cached['username'])
self.logger.info(cached['roles'])
# -*- coding: utf-8 -*-
# Zato
from zato.server.service import Service
class MyService(Service):
def handle(self):
self.cache.set('temp-key', 'temp-value')
# Delete the key
self.cache.delete('temp-key')
# Returns None because the key was deleted
value = self.cache.get('temp-key')
self.logger.info(value)
# -*- coding: utf-8 -*-
# Zato
from zato.server.service import Service
class MyService(Service):
def handle(self):
self.cache.set('flag-key', True, expiry=300)
if self.cache.exists('flag-key'):
self.logger.info('Key is present')
self.cache.delete('flag-key')
if not self.cache.exists('flag-key'):
self.logger.info('Key is gone')
# -*- coding: utf-8 -*-
# Zato
from zato.server.service import Service
class MyService(Service):
def handle(self):
# Returns None when the key does not exist
value = self.cache.get('no-such-key')
if value is None:
self.logger.info('Key not found, fetching from source ..')
value = 'value-from-source'
self.cache.set('no-such-key', value, expiry=120)
self.logger.info(value)
Book a demo with an expert who will help you build meaningful systems that match your ambitions
"For me, Zato Source is the only technology partner to help with operational improvements."