cache_mysql

cache_mysql#


class MySQLPersistentKeyValueCache(host: str, db: str, user: str, pw: str, value_type: ValueType, table_name='cache', connect_params: Optional[dict] = None, in_memory=False, max_key_length: int = 255, port=3306)[source]#

Bases: PersistentKeyValueCache

Can cache arbitrary values in a MySQL database. The keys are always strings at the database level, i.e. if a key is not a string, it is converted to a string using str().

Parameters:
  • host

  • db

  • user

  • pw

  • value_type – the type of value to store in the cache

  • table_name

  • connect_params – additional parameters to pass to the pymysql.connect() function (e.g. ssl, etc.)

  • in_memory

  • max_key_length – maximal length of the cache key string (keys are always strings) stored in the DB (i.e. the MySQL type is VARCHAR[max_key_length])

  • port – the MySQL server port to connect to

class ValueType(value)[source]#

Bases: Enum

The value type to use within the MySQL database. Note that the binary BLOB types can be used for all Python types that can be pickled, so the lack of specific types (e.g. for strings) is not a problem.

DOUBLE = ('DOUBLE', False)#
BLOB = ('BLOB', True)#

for Python data types whose pickled representation is up to 64 KB

MEDIUMBLOB = ('MEDIUMBLOB', True)#

for Python data types whose pickled representation is up to 16 MB

set(key, value)[source]#

Sets a cached value

Parameters:
  • key – the key under which to store the value

  • value – the value to store; since None is used indicate the absence of a value, None should not be used a value

get(key)[source]#

Retrieves a cached value

Parameters:

key – the lookup key

Returns:

the cached value or None if no value is found