Redis usage examples

Any programming feature Redis offers can be accessed with the client library Zato uses and the chapter below serves as a set of usage examples only, not as a complete reference.

Simple keys

1
2
3
4
5
6
from zato.server.service import Service

class MyService(Service):
    def handle(self):
        self.kvdb.conn.set('my-key', 'my-value')
        self.logger.info(self.kvdb.conn.get('my-key'))
INFO - my-value

Lists

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
from zato.server.service import Service

class MyService(Service):
    def handle(self):

        # Clear/delete the list
        self.kvdb.conn.delete('my-list')

        # Push 10 elements
        for x in range(10):
            self.kvdb.conn.lpush('my-list', x)

        # Read them back
        self.logger.info(self.kvdb.conn.lrange('my-list', 0, 9999))
INFO - ['9', '8', '7', '6', '5', '4', '3', '2', '1', '0']

Dictionaries (hashmaps)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
from zato.server.service import Service

class MyService(Service):
    def handle(self):

        # Clear/delete the dictionary
        self.kvdb.conn.delete('my-dict')

        # Set 10 keys and values
        for x in range(10):
            self.kvdb.conn.hset('my-dict', x, x**x)

        # Read one of them back
        self.logger.info(self.kvdb.conn.hget('my-dict', 7))
INFO - 823543

Transactions

Pipelines can be used to improve performance of executing multiple operations and to invoke them atomically.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
from zato.server.service import Service

class MyService(Service):
    def handle(self):

        # Either deletes all the keys specified and adds a new one or none
        # of it is performed.
        with self.kvdb.conn.pipeline() as p:

            # Deletes
            p.delete('my-key-1')
            p.delete('my-key-2')
            p.delete('my-key-3')

            # Add new key
            p.set('my-key-4', 'my-value')

            # Execute all the commands queued up in a single transaction
            p.execute()