Key-Value

❗️

This is a legacy Apache Ignite documentation

The new documentation is hosted here: https://ignite.apache.org/docs/latest/

Key-Value Operations

The pyignite.cache.Cache class provides methods to work with cache entries by using key-value operations - put, get, put all, get all, replace and others. The following example shows how to do that:

from pyignite import Client

client = Client()
client.connect('127.0.0.1', 10800)

#Create cache
my_cache = client.create_cache('my cache')

#Put value in cache
my_cache.put('my key', 42)

#Get value from cache
result = my_cache.get('my key')
print(result)  # 42

result = my_cache.get('non-existent key')
print(result)  # None

#Get multiple values from cache
result = my_cache.get_all([
    'my key',
    'non-existent key',
    'other-key',
])
print(result)  # {'my key': 42}

Using type hints

When a pyignite method or function deals with a single value or key, it has an additional parameter, like value_hint or key_hint, which accepts a parser/constructor class. Nearly any structure element (inside dict or list) can be replaced with a two-tuple of (said element, type hint).

from pyignite import Client
from pyignite.datatypes import CharObject, ShortObject

client = Client()
client.connect('127.0.0.1', 10800)

my_cache = client.get_or_create_cache('my cache')

my_cache.put('my key', 42)
# value ‘42’ takes 9 bytes of memory as a LongObject

my_cache.put('my key', 42, value_hint=ShortObject)
# value ‘42’ takes only 3 bytes as a ShortObject

my_cache.put('a', 1)
# ‘a’ is a key of type String

my_cache.put('a', 2, key_hint=CharObject) 
# another key ‘a’ of type CharObject was created

value = my_cache.get('a')
print(value) # 1

value = my_cache.get('a', key_hint=CharObject)
print(value) # 2

# now let us delete both keys at once
my_cache.remove([
    'a',                # a default type key
    ('a', CharObject),  # a key of type CharObject
])

Refer to the Data Types section for the full list of parser/constructor classes you can use as type hints.

Scan Query

Cache’s scan() method queries allows you to get the whole contents of the cache, element by element.

Let us put some data in cache.

my_cache.put_all({'key_{}'.format(v): v for v in range(20)})
# {
#     'key_0': 0,
#     'key_1': 1,
#     'key_2': 2,
#     ... 20 elements in total...
#     'key_18': 18,
#     'key_19': 19
# }

result = my_cache.scan()

The scan() method returns a generator that yields two-tuples of key and value. You can iterate through the generated pairs in a safe manner:

for k, v in result:
    print(k, v)
# 'key_17' 17
# 'key_10' 10
# 'key_6' 6,
# ... 20 elements in total...
# 'key_16' 16
# 'key_12' 12

Or, alternatively, you can convert the generator to dictionary in one go:

print(dict(result))
# {
#     'key_17': 17,
#     'key_10': 10,
#     'key_6': 6,
#     ... 20 elements in total...
#     'key_16': 16,
#     'key_12': 12
# }

Be cautious: if the cache contains a large set of data, the dictionary may consume too much memory!

Do cleanup

Destroy created cache and close connection.

my_cache.destroy()
client.close()

📘

Python example files

Python thin client contains fully workable examples to demonstrate the behavior of the client.