Caching

The caching module provides flexible caching mechanisms to improve performance and reduce API calls.

Caching Module for BingX API.

This module provides synchronous and asynchronous caching implementations for the BingX API. It includes in-memory and Redis-based caches, with support for time-to-live (TTL) and thread-safe operations.

Classes:
  • BaseCache: Abstract base class for synchronous caches.

  • BaseAsyncCache: Abstract base class for asynchronous caches.

  • SyncMemoryCache: Synchronous in-memory cache implementation.

  • AsyncMemoryCache: Asynchronous in-memory cache implementation.

  • SyncRedisCache: Synchronous Redis cache implementation.

  • AsyncRedisCache: Asynchronous Redis cache implementation.

from bingx_py.caching import SyncMemoryCache, AsyncMemoryCache

# Synchronous in-memory cache
sync_cache = SyncMemoryCache()
sync_cache.set("key", {"data": "value"}, ttl=60)
data = sync_cache.get("key")

# Asynchronous in-memory cache
async_cache = AsyncMemoryCache()
await async_cache.aset("key", {"data": "value"}, ttl=60)
data = await async_cache.aget("key")
class bingx_py.caching.AsyncMemoryCache[source]

Bases: BaseAsyncCache

Asynchronous in-memory cache implementation.

This class provides an async-safe in-memory cache implementation. It provides methods to set and get data with optional TTL (time-to-live) support.

async aget(key)[source]

Get a value from the cache asynchronously.

Parameters:

key (str) – The key to retrieve the value for.

Returns:

The value associated with the key, or None if not found or expired.

Return type:

Optional[dict[str, Any]]

async aset(key, value, ttl=None)[source]

Set a value in the cache asynchronously.

Parameters:
  • key (str) – The key to associate the value with.

  • value (dict[str, Any]) – The value to store.

  • ttl (Optional[int]) – The time-to-live in seconds. Defaults to None.

Returns:

None

Return type:

None

class bingx_py.caching.AsyncRedisCache(host='localhost', port=6379, db=0)[source]

Bases: BaseAsyncCache

Asynchronous Redis cache implementation.

This class provides an asynchronous Redis cache implementation using the redis.asyncio library. It provides methods to set and get data from Redis, with optional TTL (time-to-live) support.

Parameters:
  • host (str)

  • port (int)

  • db (int)

async aget(key)[source]

Get data from Redis asynchronously.

Parameters:

key (str) – The key to retrieve from Redis.

Returns:

The retrieved data, or None if the key does not exist.

Return type:

Optional[dict[str, Any]]

async aset(key, value, ttl=None)[source]

Set data in Redis asynchronously.

Parameters:
  • key (str) – The key to set in Redis.

  • value (dict[str, Any]) – The data to set.

  • ttl (Optional[int]) – The time-to-live (TTL) for the data in seconds. If not provided, the data will not expire.

Returns:

None

Return type:

None

class bingx_py.caching.BaseAsyncCache[source]

Bases: ABC

Base class for asynchronous caches.

This class provides a basic interface for asynchronous caches. It provides methods to set and get data from the cache, with optional TTL (time-to-live) support.

abstract async aget(key)[source]

Get data from the cache asynchronously.

Parameters:

key (str) – The key to retrieve from the cache.

Returns:

The retrieved data, or None if the key does not exist.

Return type:

Optional[dict[str, Any]]

abstract async aset(key, value, ttl=None)[source]

Set data in the cache asynchronously.

Parameters:
  • key (str) – The key to set in the cache.

  • value (dict[str, Any]) – The data to set.

  • ttl (Optional[int]) – The time-to-live (TTL) for the data in seconds. If not provided, the data will not expire.

Returns:

None

Return type:

None

class bingx_py.caching.BaseCache[source]

Bases: ABC

Base class for synchronous caches.

This class provides a basic interface for synchronous caches. It provides methods to set and get data from the cache, with optional TTL (time-to-live) support.

abstract get(key)[source]

Get data from the cache.

Parameters:

key (str) – The key to retrieve from the cache.

Returns:

The retrieved data, or None if the key does not exist.

Return type:

Optional[dict[str, Any]]

abstract set(key, value, ttl=None)[source]

Set data in the cache.

Parameters:
  • key (str) – The key to set in the cache.

  • value (dict[str, Any]) – The data to set.

  • ttl (Optional[int]) – The time-to-live (TTL) for the data in seconds. If not provided, the data will not expire.

Returns:

None

Return type:

None

class bingx_py.caching.SyncMemoryCache[source]

Bases: BaseCache

Synchronous in-memory cache implementation.

This class provides a thread-safe synchronous in-memory cache implementation. It provides methods to set and get data with optional TTL (time-to-live) support.

get(key)[source]

Get a value from the cache.

Parameters:

key (str) – The key to retrieve the value for.

Returns:

The value associated with the key, or None if not found or expired.

Return type:

Optional[dict[str, Any]]

set(key, value, ttl=None)[source]

Set a value in the cache.

Parameters:
  • key (str) – The key to associate the value with.

  • value (dict[str, Any]) – The value to store.

  • ttl (Optional[int]) – The time-to-live in seconds. Defaults to None.

Returns:

None

Return type:

None

class bingx_py.caching.SyncRedisCache(host='localhost', port=6379, db=0)[source]

Bases: BaseCache

Synchronous Redis cache implementation.

This class provides a synchronous Redis cache implementation using the redis library. It provides methods to set and get data from Redis, with optional TTL (time-to-live) support.

Parameters:
  • host (str)

  • port (int)

  • db (int)

get(key)[source]

Get data from Redis.

Parameters:

key (str) – The key to retrieve from Redis.

Returns:

The retrieved data, or None if the key does not exist.

Return type:

Optional[dict[str, Any]]

set(key, value, ttl=None)[source]

Set data in Redis.

Parameters:
  • key (str) – The key to set in Redis.

  • value (dict[str, Any]) – The data to set.

  • ttl (Optional[int]) – The time-to-live (TTL) for the data in seconds. If not provided, the data will not expire.

Returns:

None

Return type:

None