Schedule a demo

Python cache programming

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.

Setting and getting a value

# -*- 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)
INFO - my-value

Setting a value with expiry

# -*- 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)
INFO - abc123

Caching a dictionary

# -*- 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'])
INFO - joe
INFO - ['admin', 'editor']

Deleting a key

# -*- 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)
INFO - None

Checking whether a key exists

# -*- 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')
INFO - Key is present
INFO - Key is gone

Cache miss

# -*- 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)
INFO - Key not found, fetching from source ..
INFO - value-from-source


Schedule a meaningful demo

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."

- John Adams
Program Manager of Channel Enablement at Keysight