pygridgain.cache module

class pygridgain.cache.BaseCache(client: BaseClient, name: str, expiry_policy: ExpiryPolicy = None)
__init__(client: BaseClient, name: str, expiry_policy: ExpiryPolicy = None)
property cache_id: int

Cache ID.

Returns:

integer value of the cache ID.

property cache_info: CacheInfo

Cache meta info.

property client: BaseClient
Returns:

Client object, through which the cache is accessed.

property name: str
Returns:

cache name string.

with_expire_policy(expiry_policy: ExpiryPolicy | None = None, create: int | timedelta = -2, update: int | timedelta = -2, access: int | timedelta = -2)
Parameters:
  • expiry_policy – optional ExpiryPolicy object. If it is set, other params will be ignored,

  • create – TTL for create in milliseconds or timedelta,

  • update – TTL for update in milliseconds or timedelta,

  • access – TTL for access in milliseconds or timedelta,

Returns:

cache decorator with expiry policy set.

class pygridgain.cache.Cache(client: Client, name: str, expiry_policy: ExpiryPolicy = None)

GridGain cache abstraction. Users should never use this class directly, but construct its instances with create_cache(), get_or_create_cache() or get_cache() methods instead. See this example on how to do it.

__init__(client: Client, name: str, expiry_policy: ExpiryPolicy = None)

Initialize cache object. For internal use.

Parameters:
  • client – Ignite client,

  • name – Cache name.

property cache_id: int

Cache ID.

Returns:

integer value of the cache ID.

property cache_info: CacheInfo

Cache meta info.

clear(keys: list | None = None)

Clears the cache without notifying listeners or cache writers.

Parameters:

keys – (optional) list of cache keys or (key, key type hint) tuples to clear (default: clear all).

clear_key(key, key_hint: object = None)

Clears the cache key without notifying listeners or cache writers.

Parameters:
  • key – key for the cache entry,

  • key_hint – (optional) GridGain data type, for which the given key should be converted,

clear_keys(keys: Iterable)

Clears the cache key without notifying listeners or cache writers.

Parameters:

keys – a list of keys or (key, type hint) tuples

property client: BaseClient
Returns:

Client object, through which the cache is accessed.

contains_key(key, key_hint=None) bool

Returns a value indicating whether given key is present in cache.

Parameters:
  • key – key for the cache entry. Can be of any supported type,

  • key_hint – (optional) GridGain data type, for which the given key should be converted,

Returns:

boolean True when key is present, False otherwise.

contains_keys(keys: Iterable) bool

Returns a value indicating whether all given keys are present in cache.

Parameters:

keys – a list of keys or (key, type hint) tuples,

Returns:

boolean True when all keys are present, False otherwise.

destroy()

Destroys cache with a given name.

get(key, key_hint: object = None) Any

Retrieves a value from cache by key.

Parameters:
  • key – key for the cache entry. Can be of any supported type,

  • key_hint – (optional) GridGain data type, for which the given key should be converted,

Returns:

value retrieved.

get_all(keys: list) dict

Retrieves multiple key-value pairs from cache.

Parameters:

keys – list of keys or tuples of (key, key_hint),

Returns:

a dict of key-value pairs.

get_and_put(key, value, key_hint=None, value_hint=None) Any

Puts a value with a given key to cache, and returns the previous value for that key, or null value if there was not such key.

Parameters:
  • key – key for the cache entry. Can be of any supported type,

  • value – value for the key,

  • key_hint – (optional) GridGain data type, for which the given key should be converted,

  • value_hint – (optional) GridGain data type, for which the given value should be converted.

Returns:

old value or None.

get_and_put_if_absent(key, value, key_hint=None, value_hint=None)

Puts a value with a given key to cache only if the key does not already exist.

Parameters:
  • key – key for the cache entry. Can be of any supported type,

  • value – value for the key,

  • key_hint – (optional) GridGain data type, for which the given key should be converted,

  • value_hint – (optional) GridGain data type, for which the given value should be converted,

Returns:

old value or None.

get_and_remove(key, key_hint=None) Any

Removes the cache entry with specified key, returning the value.

Parameters:
  • key – key for the cache entry. Can be of any supported type,

  • key_hint – (optional) GridGain data type, for which the given key should be converted,

Returns:

old value or None.

get_and_replace(key, value, key_hint=None, value_hint=None) Any

Puts a value with a given key to cache, returning previous value for that key, if and only if there is a value currently mapped for that key.

Parameters:
  • key – key for the cache entry. Can be of any supported type,

  • value – value for the key,

  • key_hint – (optional) GridGain data type, for which the given key should be converted,

  • value_hint – (optional) GridGain data type, for which the given value should be converted.

Returns:

old value or None.

get_size(peek_modes=None)

Gets the number of entries in cache.

Parameters:

peek_modes – (optional) limit count to near cache partition (PeekModes.NEAR), primary cache (PeekModes.PRIMARY), or backup cache (PeekModes.BACKUP). Defaults to primary cache partitions (PeekModes.PRIMARY),

Returns:

integer number of cache entries.

property name: str
Returns:

cache name string.

put(key, value, key_hint: object = None, value_hint: object = None)

Puts a value with a given key to cache (overwriting existing value if any).

Parameters:
  • key – key for the cache entry. Can be of any supported type,

  • value – value for the key,

  • key_hint – (optional) GridGain data type, for which the given key should be converted,

  • value_hint – (optional) GridGain data type, for which the given value should be converted.

put_all(pairs: dict)

Puts multiple key-value pairs to cache (overwriting existing associations if any).

Parameters:

pairs – dictionary type parameters, contains key-value pairs to save. Each key or value can be an item of representable Python type or a tuple of (item, hint),

put_if_absent(key, value, key_hint=None, value_hint=None)

Puts a value with a given key to cache only if the key does not already exist.

Parameters:
  • key – key for the cache entry. Can be of any supported type,

  • value – value for the key,

  • key_hint – (optional) GridGain data type, for which the given key should be converted,

  • value_hint – (optional) GridGain data type, for which the given value should be converted.

remove_all()

Removes all cache entries, notifying listeners and cache writers.

remove_if_equals(key, sample, key_hint=None, sample_hint=None)

Removes an entry with a given key if provided value is equal to actual value, notifying listeners and cache writers.

Parameters:
  • key – key for the cache entry,

  • sample – a sample to compare the stored value with,

  • key_hint – (optional) GridGain data type, for which the given key should be converted,

  • sample_hint – (optional) GridGain data type, for whic the given sample should be converted.

remove_key(key, key_hint=None)

Clears the cache key without notifying listeners or cache writers.

Parameters:
  • key – key for the cache entry,

  • key_hint – (optional) GridGain data type, for which the given key should be converted,

remove_keys(keys: list)

Removes cache entries by given list of keys, notifying listeners and cache writers.

Parameters:

keys – list of keys or tuples of (key, key_hint) to remove.

replace(key, value, key_hint: object = None, value_hint: object = None)

Puts a value with a given key to cache only if the key already exist.

Parameters:
  • key – key for the cache entry. Can be of any supported type,

  • value – value for the key,

  • key_hint – (optional) GridGain data type, for which the given key should be converted,

  • value_hint – (optional) GridGain data type, for which the given value should be converted.

replace_if_equals(key, sample, value, key_hint=None, sample_hint=None, value_hint=None) Any

Puts a value with a given key to cache only if the key already exists and value equals provided sample.

Parameters:
  • key – key for the cache entry,

  • sample – a sample to compare the stored value with,

  • value – new value for the given key,

  • key_hint – (optional) GridGain data type, for which the given key should be converted,

  • sample_hint – (optional) GridGain data type, for which the given sample should be converted

  • value_hint – (optional) GridGain data type, for which the given value should be converted,

Returns:

boolean True when key is present, False otherwise.

scan(page_size: int = 1, partitions: int = -1, local: bool = False) ScanCursor

Returns all key-value pairs from the cache, similar to get_all, but with internal pagination, which is slower, but safer.

Parameters:
  • page_size – (optional) page size. Default size is 1 (slowest and safest),

  • partitions – (optional) number of partitions to query (negative to query entire cache),

  • local – (optional) pass True if this query should be executed on local node only. Defaults to False,

Returns:

Scan query cursor.

select_row(query_str: str, page_size: int = 1, query_args: list | None = None, distributed_joins: bool = False, replicated_only: bool = False, local: bool = False, timeout: int = 0) SqlCursor

Executes a simplified SQL SELECT query over data stored in the cache. The query returns the whole record (key and value).

Parameters:
  • query_str – SQL query string,

  • page_size – (optional) cursor page size. Default is 1, which means that client makes one server call per row,

  • query_args – (optional) query arguments,

  • distributed_joins – (optional) distributed joins. Defaults to False,

  • replicated_only – (optional) whether query contains only replicated tables or not. Defaults to False,

  • local – (optional) pass True if this query should be executed on local node only. Defaults to False,

  • timeout – (optional) non-negative timeout value in ms. Zero disables timeout (default),

Returns:

Sql cursor.

property settings: dict | None

Lazy Cache settings. See the example of reading this property.

All cache properties are documented here: Cache Properties.

Returns:

dict of cache properties and their values.

vector(type_name: str, field: str, clause_vector: List[float], k: int, threshold: float, page_size: int = 1) VectorCursor

Ignite supports vector queries based on Apache Lucene engine.

Parameters:
  • type_name – Name of the type.

  • field – Name of the field.

  • clause_vector – Search vector.

  • k – [K]NN, how many vectors to return.

  • page_size – (optional) page size. Default size is 1 (slowest and safest),

Returns:

Scan query cursor.

with_expire_policy(expiry_policy: ExpiryPolicy | None = None, create: int | timedelta = -2, update: int | timedelta = -2, access: int | timedelta = -2)
Parameters:
  • expiry_policy – optional ExpiryPolicy object. If it is set, other params will be ignored,

  • create – TTL for create in milliseconds or timedelta,

  • update – TTL for update in milliseconds or timedelta,

  • access – TTL for access in milliseconds or timedelta,

Returns:

cache decorator with expiry policy set.

pygridgain.cache.create_cache(client: Client, settings: str | dict) Cache
pygridgain.cache.get_cache(client: Client, settings: str | dict) Cache
pygridgain.cache.get_or_create_cache(client: Client, settings: str | dict) Cache