{
  "id": "cache",
  "title": "LLM Cache",
  "url": "https://redis.io/docs/latest/develop/ai/redisvl/0.11.1/api/cache/",
  "summary": "",
  "content": "\n\n## SemanticCache\n\n\u003ca id=\"semantic-cache-api\"\u003e\u003c/a\u003e\n\n### `class SemanticCache(name='llmcache', distance_threshold=0.1, ttl=None, vectorizer=None, filterable_fields=None, redis_client=None, redis_url='redis://localhost:6379', connection_kwargs={}, overwrite=False, **kwargs)`\n\nBases: `BaseLLMCache`\n\nSemantic Cache for Large Language Models.\n\nSemantic Cache for Large Language Models.\n\n* **Parameters:**\n  * **name** (*str* *,* *optional*) – The name of the semantic cache search index.\n    Defaults to \"llmcache\".\n  * **distance_threshold** (*float* *,* *optional*) – Semantic threshold for the\n    cache. Defaults to 0.1.\n  * **ttl** (*Optional* *[* *int* *]* *,* *optional*) – The time-to-live for records cached\n    in Redis. Defaults to None.\n  * **vectorizer** (*Optional* *[* *BaseVectorizer* *]* *,* *optional*) – The vectorizer for the cache.\n    Defaults to HFTextVectorizer.\n  * **filterable_fields** (*Optional* *[* *List* *[* *Dict* *[* *str* *,* *Any* *]* *]* *]*) – An optional list of RedisVL fields\n    that can be used to customize cache retrieval with filters.\n  * **redis_client** (*Optional* *[* *Redis* *]* *,* *optional*) – A redis client connection instance.\n    Defaults to None.\n  * **redis_url** (*str* *,* *optional*) – The redis url. Defaults to redis://localhost:6379.\n  * **connection_kwargs** (*Dict* *[* *str* *,* *Any* *]*) – The connection arguments\n    for the redis client. Defaults to empty {}.\n  * **overwrite** (*bool*) – Whether or not to force overwrite the schema for\n    the semantic cache index. Defaults to false.\n* **Raises:**\n  * **TypeError** – If an invalid vectorizer is provided.\n  * **TypeError** – If the TTL value is not an int.\n  * **ValueError** – If the threshold is not between 0 and 1.\n  * **ValueError** – If existing schema does not match new schema and overwrite is False.\n\n#### `async acheck(prompt=None, vector=None, num_results=1, return_fields=None, filter_expression=None, distance_threshold=None)`\n\nAsync check the semantic cache for results similar to the specified prompt\nor vector.\n\nThis method searches the cache using vector similarity with\neither a raw text prompt (converted to a vector) or a provided vector as\ninput. It checks for semantically similar prompts and fetches the cached\nLLM responses.\n\n* **Parameters:**\n  * **prompt** (*Optional* *[* *str* *]* *,* *optional*) – The text prompt to search for in\n    the cache.\n  * **vector** (*Optional* *[* *List* *[* *float* *]* *]* *,* *optional*) – The vector representation\n    of the prompt to search for in the cache.\n  * **num_results** (*int* *,* *optional*) – The number of cached results to return.\n    Defaults to 1.\n  * **return_fields** (*Optional* *[* *List* *[* *str* *]* *]* *,* *optional*) – The fields to include\n    in each returned result. If None, defaults to all available\n    fields in the cached entry.\n  * **filter_expression** (*Optional* *[*[*FilterExpression*]() *]*) – Optional filter expression\n    that can be used to filter cache results. Defaults to None and\n    the full cache will be searched.\n  * **distance_threshold** (*Optional* *[* *float* *]*) – The threshold for semantic\n    vector distance.\n* **Returns:**\n  A list of dicts containing the requested\n  : return fields for each similar cached response.\n* **Return type:**\n  List[Dict[str, Any]]\n* **Raises:**\n  * **ValueError** – If neither a prompt nor a vector is specified.\n  * **ValueError** – if ‘vector’ has incorrect dimensions.\n  * **TypeError** – If return_fields is not a list when provided.\n\n```python\nresponse = await cache.acheck(\n    prompt=\"What is the capital city of France?\"\n)\n```\n\n#### `async aclear()`\n\nAsync clear the cache of all keys.\n\n* **Return type:**\n  None\n\n#### `async adelete()`\n\nAsync delete the cache and its index entirely.\n\n* **Return type:**\n  None\n\n#### `async adisconnect()`\n\nAsynchronously disconnect from Redis and search index.\n\nCloses all Redis connections and index connections.\n\n#### `async adrop(ids=None, keys=None)`\n\nAsync drop specific entries from the cache by ID or Redis key.\n\n* **Parameters:**\n  * **ids** (*Optional* *[* *List* *[* *str* *]* *]*) – List of entry IDs to remove from the cache.\n    Entry IDs are the unique identifiers without the cache prefix.\n  * **keys** (*Optional* *[* *List* *[* *str* *]* *]*) – List of full Redis keys to remove from the cache.\n    Keys are the complete Redis keys including the cache prefix.\n* **Return type:**\n  None\n\n#### `NOTE`\nAt least one of ids or keys must be provided.\n\n* **Raises:**\n  **ValueError** – If neither ids nor keys is provided.\n* **Parameters:**\n  * **ids** (*List* *[* *str* *]*  *|* *None*)\n  * **keys** (*List* *[* *str* *]*  *|* *None*)\n* **Return type:**\n  None\n\n#### `async aexpire(key, ttl=None)`\n\nAsynchronously set or refresh the expiration time for a key in the cache.\n\n* **Parameters:**\n  * **key** (*str*) – The Redis key to set the expiration on.\n  * **ttl** (*Optional* *[* *int* *]* *,* *optional*) – The time-to-live in seconds. If None,\n    uses the default TTL configured for this cache instance.\n    Defaults to None.\n* **Return type:**\n  None\n\n#### `NOTE`\nIf neither the provided TTL nor the default TTL is set (both are None),\nthis method will have no effect.\n\n#### `async astore(prompt, response, vector=None, metadata=None, filters=None, ttl=None)`\n\nAsync stores the specified key-value pair in the cache along with metadata.\n\n* **Parameters:**\n  * **prompt** (*str*) – The user prompt to cache.\n  * **response** (*str*) – The LLM response to cache.\n  * **vector** (*Optional* *[* *List* *[* *float* *]* *]* *,* *optional*) – The prompt vector to\n    cache. Defaults to None, and the prompt vector is generated on\n    demand.\n  * **metadata** (*Optional* *[* *Dict* *[* *str* *,* *Any* *]* *]* *,* *optional*) – The optional metadata to cache\n    alongside the prompt and response. Defaults to None.\n  * **filters** (*Optional* *[* *Dict* *[* *str* *,* *Any* *]* *]*) – The optional tag to assign to the cache entry.\n    Defaults to None.\n  * **ttl** (*Optional* *[* *int* *]*) – The optional TTL override to use on this individual cache\n    entry. Defaults to the global TTL setting.\n* **Returns:**\n  The Redis key for the entries added to the semantic cache.\n* **Return type:**\n  str\n* **Raises:**\n  * **ValueError** – If neither prompt nor vector is specified.\n  * **ValueError** – if vector has incorrect dimensions.\n  * **TypeError** – If provided metadata is not a dictionary.\n\n```python\nkey = await cache.astore(\n    prompt=\"What is the capital city of France?\",\n    response=\"Paris\",\n    metadata={\"city\": \"Paris\", \"country\": \"France\"}\n)\n```\n\n#### `async aupdate(key, **kwargs)`\n\nAsync update specific fields within an existing cache entry. If no fields\nare passed, then only the document TTL is refreshed.\n\n* **Parameters:**\n  **key** (*str*) – the key of the document to update using kwargs.\n* **Raises:**\n  * **ValueError if an incorrect mapping is provided as a kwarg.** – \n  * **TypeError if metadata is provided and not** **of** **type dict.** – \n* **Return type:**\n  None\n\n```python\nkey = await cache.astore('this is a prompt', 'this is a response')\nawait cache.aupdate(\n    key,\n    metadata={\"hit_count\": 1, \"model_name\": \"Llama-2-7b\"}\n)\n```\n\n#### `check(prompt=None, vector=None, num_results=1, return_fields=None, filter_expression=None, distance_threshold=None)`\n\nChecks the semantic cache for results similar to the specified prompt\nor vector.\n\nThis method searches the cache using vector similarity with\neither a raw text prompt (converted to a vector) or a provided vector as\ninput. It checks for semantically similar prompts and fetches the cached\nLLM responses.\n\n* **Parameters:**\n  * **prompt** (*Optional* *[* *str* *]* *,* *optional*) – The text prompt to search for in\n    the cache.\n  * **vector** (*Optional* *[* *List* *[* *float* *]* *]* *,* *optional*) – The vector representation\n    of the prompt to search for in the cache.\n  * **num_results** (*int* *,* *optional*) – The number of cached results to return.\n    Defaults to 1.\n  * **return_fields** (*Optional* *[* *List* *[* *str* *]* *]* *,* *optional*) – The fields to include\n    in each returned result. If None, defaults to all available\n    fields in the cached entry.\n  * **filter_expression** (*Optional* *[*[*FilterExpression*]() *]*) – Optional filter expression\n    that can be used to filter cache results. Defaults to None and\n    the full cache will be searched.\n  * **distance_threshold** (*Optional* *[* *float* *]*) – The threshold for semantic\n    vector distance.\n* **Returns:**\n  A list of dicts containing the requested\n  : return fields for each similar cached response.\n* **Return type:**\n  List[Dict[str, Any]]\n* **Raises:**\n  * **ValueError** – If neither a prompt nor a vector is specified.\n  * **ValueError** – if ‘vector’ has incorrect dimensions.\n  * **TypeError** – If return_fields is not a list when provided.\n\n```python\nresponse = cache.check(\n    prompt=\"What is the capital city of France?\"\n)\n```\n\n#### `clear()`\n\nClear the cache of all keys.\n\n* **Return type:**\n  None\n\n#### `delete()`\n\nDelete the cache and its index entirely.\n\n* **Return type:**\n  None\n\n#### `disconnect()`\n\nDisconnect from Redis and search index.\n\nCloses all Redis connections and index connections.\n\n#### `drop(ids=None, keys=None)`\n\nDrop specific entries from the cache by ID or Redis key.\n\n* **Parameters:**\n  * **ids** (*Optional* *[* *List* *[* *str* *]* *]*) – List of entry IDs to remove from the cache.\n    Entry IDs are the unique identifiers without the cache prefix.\n  * **keys** (*Optional* *[* *List* *[* *str* *]* *]*) – List of full Redis keys to remove from the cache.\n    Keys are the complete Redis keys including the cache prefix.\n* **Return type:**\n  None\n\n#### `NOTE`\nAt least one of ids or keys must be provided.\n\n* **Raises:**\n  **ValueError** – If neither ids nor keys is provided.\n* **Parameters:**\n  * **ids** (*List* *[* *str* *]*  *|* *None*)\n  * **keys** (*List* *[* *str* *]*  *|* *None*)\n* **Return type:**\n  None\n\n#### `expire(key, ttl=None)`\n\nSet or refresh the expiration time for a key in the cache.\n\n* **Parameters:**\n  * **key** (*str*) – The Redis key to set the expiration on.\n  * **ttl** (*Optional* *[* *int* *]* *,* *optional*) – The time-to-live in seconds. If None,\n    uses the default TTL configured for this cache instance.\n    Defaults to None.\n* **Return type:**\n  None\n\n#### `NOTE`\nIf neither the provided TTL nor the default TTL is set (both are None),\nthis method will have no effect.\n\n#### `set_threshold(distance_threshold)`\n\nSets the semantic distance threshold for the cache.\n\n* **Parameters:**\n  **distance_threshold** (*float*) – The semantic distance threshold for\n  the cache.\n* **Raises:**\n  **ValueError** – If the threshold is not between 0 and 1.\n* **Return type:**\n  None\n\n#### `set_ttl(ttl=None)`\n\nSet the default TTL, in seconds, for entries in the cache.\n\n* **Parameters:**\n  **ttl** (*Optional* *[* *int* *]* *,* *optional*) – The optional time-to-live expiration\n  for the cache, in seconds.\n* **Raises:**\n  **ValueError** – If the time-to-live value is not an integer.\n* **Return type:**\n  None\n\n#### `store(prompt, response, vector=None, metadata=None, filters=None, ttl=None)`\n\nStores the specified key-value pair in the cache along with metadata.\n\n* **Parameters:**\n  * **prompt** (*str*) – The user prompt to cache.\n  * **response** (*str*) – The LLM response to cache.\n  * **vector** (*Optional* *[* *List* *[* *float* *]* *]* *,* *optional*) – The prompt vector to\n    cache. Defaults to None, and the prompt vector is generated on\n    demand.\n  * **metadata** (*Optional* *[* *Dict* *[* *str* *,* *Any* *]* *]* *,* *optional*) – The optional metadata to cache\n    alongside the prompt and response. Defaults to None.\n  * **filters** (*Optional* *[* *Dict* *[* *str* *,* *Any* *]* *]*) – The optional tag to assign to the cache entry.\n    Defaults to None.\n  * **ttl** (*Optional* *[* *int* *]*) – The optional TTL override to use on this individual cache\n    entry. Defaults to the global TTL setting.\n* **Returns:**\n  The Redis key for the entries added to the semantic cache.\n* **Return type:**\n  str\n* **Raises:**\n  * **ValueError** – If neither prompt nor vector is specified.\n  * **ValueError** – if vector has incorrect dimensions.\n  * **TypeError** – If provided metadata is not a dictionary.\n\n```python\nkey = cache.store(\n    prompt=\"What is the capital city of France?\",\n    response=\"Paris\",\n    metadata={\"city\": \"Paris\", \"country\": \"France\"}\n)\n```\n\n#### `update(key, **kwargs)`\n\nUpdate specific fields within an existing cache entry. If no fields\nare passed, then only the document TTL is refreshed.\n\n* **Parameters:**\n  **key** (*str*) – the key of the document to update using kwargs.\n* **Raises:**\n  * **ValueError if an incorrect mapping is provided as a kwarg.** – \n  * **TypeError if metadata is provided and not** **of** **type dict.** – \n* **Return type:**\n  None\n\n```python\nkey = cache.store('this is a prompt', 'this is a response')\ncache.update(key, metadata={\"hit_count\": 1, \"model_name\": \"Llama-2-7b\"})\n```\n\n#### `property aindex: `[`AsyncSearchIndex`]()`  | None`\n\nThe underlying AsyncSearchIndex for the cache.\n\n* **Returns:**\n  The async search index.\n* **Return type:**\n  [AsyncSearchIndex]()\n\n#### `property distance_threshold: float`\n\nThe semantic distance threshold for the cache.\n\n* **Returns:**\n  The semantic distance threshold.\n* **Return type:**\n  float\n\n#### `property index: `[`SearchIndex`]()` `\n\nThe underlying SearchIndex for the cache.\n\n* **Returns:**\n  The search index.\n* **Return type:**\n  [SearchIndex]()\n\n#### `property ttl: int | None`\n\nThe default TTL, in seconds, for entries in the cache.\n\n# Embeddings Cache\n\n## EmbeddingsCache\n\n\u003ca id=\"embeddings-cache-api\"\u003e\u003c/a\u003e\n\n### `class EmbeddingsCache(name='embedcache', ttl=None, redis_client=None, async_redis_client=None, redis_url='redis://localhost:6379', connection_kwargs={})`\n\nBases: `BaseCache`\n\nEmbeddings Cache for storing embedding vectors with exact key matching.\n\nInitialize an embeddings cache.\n\n* **Parameters:**\n  * **name** (*str*) – The name of the cache. Defaults to \"embedcache\".\n  * **ttl** (*Optional* *[* *int* *]*) – The time-to-live for cached embeddings. Defaults to None.\n  * **redis_client** (*Optional* *[* *SyncRedisClient* *]*) – Redis client instance. Defaults to None.\n  * **redis_url** (*str*) – Redis URL for connection. Defaults to \"redis://localhost:6379\".\n  * **connection_kwargs** (*Dict* *[* *str* *,* *Any* *]*) – Redis connection arguments. Defaults to {}.\n  * **async_redis_client** (*Redis* *|* *RedisCluster* *|* *None*)\n* **Raises:**\n  **ValueError** – If vector dimensions are invalid\n\n```python\ncache = EmbeddingsCache(\n    name=\"my_embeddings_cache\",\n    ttl=3600,  # 1 hour\n    redis_url=\"redis://localhost:6379\"\n)\n```\n\n#### `async aclear()`\n\nAsync clear the cache of all keys.\n\n* **Return type:**\n  None\n\n#### `async adisconnect()`\n\nAsync disconnect from Redis.\n\n* **Return type:**\n  None\n\n#### `async adrop(text, model_name)`\n\nAsync remove an embedding from the cache.\n\nAsynchronously removes an embedding from the cache.\n\n* **Parameters:**\n  * **text** (*str*) – The text input that was embedded.\n  * **model_name** (*str*) – The name of the embedding model.\n* **Return type:**\n  None\n\n```python\nawait cache.adrop(\n    text=\"What is machine learning?\",\n    model_name=\"text-embedding-ada-002\"\n)\n```\n\n#### `async adrop_by_key(key)`\n\nAsync remove an embedding from the cache by its Redis key.\n\nAsynchronously removes an embedding from the cache by its Redis key.\n\n* **Parameters:**\n  **key** (*str*) – The full Redis key for the embedding.\n* **Return type:**\n  None\n\n```python\nawait cache.adrop_by_key(\"embedcache:1234567890abcdef\")\n```\n\n#### `async aexists(text, model_name)`\n\nAsync check if an embedding exists.\n\nAsynchronously checks if an embedding exists for the given text and model.\n\n* **Parameters:**\n  * **text** (*str*) – The text input that was embedded.\n  * **model_name** (*str*) – The name of the embedding model.\n* **Returns:**\n  True if the embedding exists in the cache, False otherwise.\n* **Return type:**\n  bool\n\n```python\nif await cache.aexists(\"What is machine learning?\", \"text-embedding-ada-002\"):\n    print(\"Embedding is in cache\")\n```\n\n#### `async aexists_by_key(key)`\n\nAsync check if an embedding exists for the given Redis key.\n\nAsynchronously checks if an embedding exists for the given Redis key.\n\n* **Parameters:**\n  **key** (*str*) – The full Redis key for the embedding.\n* **Returns:**\n  True if the embedding exists in the cache, False otherwise.\n* **Return type:**\n  bool\n\n```python\nif await cache.aexists_by_key(\"embedcache:1234567890abcdef\"):\n    print(\"Embedding is in cache\")\n```\n\n#### `async aexpire(key, ttl=None)`\n\nAsynchronously set or refresh the expiration time for a key in the cache.\n\n* **Parameters:**\n  * **key** (*str*) – The Redis key to set the expiration on.\n  * **ttl** (*Optional* *[* *int* *]* *,* *optional*) – The time-to-live in seconds. If None,\n    uses the default TTL configured for this cache instance.\n    Defaults to None.\n* **Return type:**\n  None\n\n#### `NOTE`\nIf neither the provided TTL nor the default TTL is set (both are None),\nthis method will have no effect.\n\n#### `async aget(text, model_name)`\n\nAsync get embedding by text and model name.\n\nAsynchronously retrieves a cached embedding for the given text and model name.\nIf found, refreshes the TTL of the entry.\n\n* **Parameters:**\n  * **text** (*str*) – The text input that was embedded.\n  * **model_name** (*str*) – The name of the embedding model.\n* **Returns:**\n  Embedding cache entry or None if not found.\n* **Return type:**\n  Optional[Dict[str, Any]]\n\n```python\nembedding_data = await cache.aget(\n    text=\"What is machine learning?\",\n    model_name=\"text-embedding-ada-002\"\n)\n```\n\n#### `async aget_by_key(key)`\n\nAsync get embedding by its full Redis key.\n\nAsynchronously retrieves a cached embedding for the given Redis key.\nIf found, refreshes the TTL of the entry.\n\n* **Parameters:**\n  **key** (*str*) – The full Redis key for the embedding.\n* **Returns:**\n  Embedding cache entry or None if not found.\n* **Return type:**\n  Optional[Dict[str, Any]]\n\n```python\nembedding_data = await cache.aget_by_key(\"embedcache:1234567890abcdef\")\n```\n\n#### `async amdrop(texts, model_name)`\n\nAsync remove multiple embeddings from the cache by their texts and model name.\n\nAsynchronously removes multiple embeddings in a single operation.\n\n* **Parameters:**\n  * **texts** (*List* *[* *str* *]*) – List of text inputs that were embedded.\n  * **model_name** (*str*) – The name of the embedding model.\n* **Return type:**\n  None\n\n```python\n# Remove multiple embeddings asynchronously\nawait cache.amdrop(\n    texts=[\"What is machine learning?\", \"What is deep learning?\"],\n    model_name=\"text-embedding-ada-002\"\n)\n```\n\n#### `async amdrop_by_keys(keys)`\n\nAsync remove multiple embeddings from the cache by their Redis keys.\n\nAsynchronously removes multiple embeddings in a single operation.\n\n* **Parameters:**\n  **keys** (*List* *[* *str* *]*) – List of Redis keys to remove.\n* **Return type:**\n  None\n\n```python\n# Remove multiple embeddings asynchronously\nawait cache.amdrop_by_keys([\"embedcache:key1\", \"embedcache:key2\"])\n```\n\n#### `async amexists(texts, model_name)`\n\nAsync check if multiple embeddings exist by their texts and model name.\n\nAsynchronously checks existence of multiple embeddings in a single operation.\n\n* **Parameters:**\n  * **texts** (*List* *[* *str* *]*) – List of text inputs that were embedded.\n  * **model_name** (*str*) – The name of the embedding model.\n* **Returns:**\n  List of boolean values indicating whether each embedding exists.\n* **Return type:**\n  List[bool]\n\n```python\n# Check if multiple embeddings exist asynchronously\nexists_results = await cache.amexists(\n    texts=[\"What is machine learning?\", \"What is deep learning?\"],\n    model_name=\"text-embedding-ada-002\"\n)\n```\n\n#### `async amexists_by_keys(keys)`\n\nAsync check if multiple embeddings exist by their Redis keys.\n\nAsynchronously checks existence of multiple keys in a single operation.\n\n* **Parameters:**\n  **keys** (*List* *[* *str* *]*) – List of Redis keys to check.\n* **Returns:**\n  List of boolean values indicating whether each key exists.\n  The order matches the input keys order.\n* **Return type:**\n  List[bool]\n\n```python\n# Check if multiple keys exist asynchronously\nexists_results = await cache.amexists_by_keys([\"embedcache:key1\", \"embedcache:key2\"])\n```\n\n#### `async amget(texts, model_name)`\n\nAsync get multiple embeddings by their texts and model name.\n\nAsynchronously retrieves multiple cached embeddings in a single operation.\nIf found, refreshes the TTL of each entry.\n\n* **Parameters:**\n  * **texts** (*List* *[* *str* *]*) – List of text inputs that were embedded.\n  * **model_name** (*str*) – The name of the embedding model.\n* **Returns:**\n  List of embedding cache entries or None for texts not found.\n* **Return type:**\n  List[Optional[Dict[str, Any]]]\n\n```python\n# Get multiple embeddings asynchronously\nembedding_data = await cache.amget(\n    texts=[\"What is machine learning?\", \"What is deep learning?\"],\n    model_name=\"text-embedding-ada-002\"\n)\n```\n\n#### `async amget_by_keys(keys)`\n\nAsync get multiple embeddings by their Redis keys.\n\nAsynchronously retrieves multiple cached embeddings in a single network roundtrip.\nIf found, refreshes the TTL of each entry.\n\n* **Parameters:**\n  **keys** (*List* *[* *str* *]*) – List of Redis keys to retrieve.\n* **Returns:**\n  List of embedding cache entries or None for keys not found.\n  The order matches the input keys order.\n* **Return type:**\n  List[Optional[Dict[str, Any]]]\n\n```python\n# Get multiple embeddings asynchronously\nembedding_data = await cache.amget_by_keys([\n    \"embedcache:key1\",\n    \"embedcache:key2\"\n])\n```\n\n#### `async amset(items, ttl=None)`\n\nAsync store multiple embeddings in a batch operation.\n\nEach item in the input list should be a dictionary with the following fields:\n- ‘text’: The text input that was embedded\n- ‘model_name’: The name of the embedding model\n- ‘embedding’: The embedding vector\n- ‘metadata’: Optional metadata to store with the embedding\n\n* **Parameters:**\n  * **items** (*List* *[* *Dict* *[* *str* *,* *Any* *]* *]*) – List of dictionaries, each containing text, model_name, embedding, and optional metadata.\n  * **ttl** (*int* *|* *None*) – Optional TTL override for these entries.\n* **Returns:**\n  List of Redis keys where the embeddings were stored.\n* **Return type:**\n  List[str]\n\n```python\n# Store multiple embeddings asynchronously\nkeys = await cache.amset([\n    {\n        \"text\": \"What is ML?\",\n        \"model_name\": \"text-embedding-ada-002\",\n        \"embedding\": [0.1, 0.2, 0.3],\n        \"metadata\": {\"source\": \"user\"}\n    },\n    {\n        \"text\": \"What is AI?\",\n        \"model_name\": \"text-embedding-ada-002\",\n        \"embedding\": [0.4, 0.5, 0.6],\n        \"metadata\": {\"source\": \"docs\"}\n    }\n])\n```\n\n#### `async aset(text, model_name, embedding, metadata=None, ttl=None)`\n\nAsync store an embedding with its text and model name.\n\nAsynchronously stores an embedding with its text and model name.\n\n* **Parameters:**\n  * **text** (*str*) – The text input that was embedded.\n  * **model_name** (*str*) – The name of the embedding model.\n  * **embedding** (*List* *[* *float* *]*) – The embedding vector to store.\n  * **metadata** (*Optional* *[* *Dict* *[* *str* *,* *Any* *]* *]*) – Optional metadata to store with the embedding.\n  * **ttl** (*Optional* *[* *int* *]*) – Optional TTL override for this specific entry.\n* **Returns:**\n  The Redis key where the embedding was stored.\n* **Return type:**\n  str\n\n```python\nkey = await cache.aset(\n    text=\"What is machine learning?\",\n    model_name=\"text-embedding-ada-002\",\n    embedding=[0.1, 0.2, 0.3, ...],\n    metadata={\"source\": \"user_query\"}\n)\n```\n\n#### `clear()`\n\nClear the cache of all keys.\n\n* **Return type:**\n  None\n\n#### `disconnect()`\n\nDisconnect from Redis.\n\n* **Return type:**\n  None\n\n#### `drop(text, model_name)`\n\nRemove an embedding from the cache.\n\n* **Parameters:**\n  * **text** (*str*) – The text input that was embedded.\n  * **model_name** (*str*) – The name of the embedding model.\n* **Return type:**\n  None\n\n```python\ncache.drop(\n    text=\"What is machine learning?\",\n    model_name=\"text-embedding-ada-002\"\n)\n```\n\n#### `drop_by_key(key)`\n\nRemove an embedding from the cache by its Redis key.\n\n* **Parameters:**\n  **key** (*str*) – The full Redis key for the embedding.\n* **Return type:**\n  None\n\n```python\ncache.drop_by_key(\"embedcache:1234567890abcdef\")\n```\n\n#### `exists(text, model_name)`\n\nCheck if an embedding exists for the given text and model.\n\n* **Parameters:**\n  * **text** (*str*) – The text input that was embedded.\n  * **model_name** (*str*) – The name of the embedding model.\n* **Returns:**\n  True if the embedding exists in the cache, False otherwise.\n* **Return type:**\n  bool\n\n```python\nif cache.exists(\"What is machine learning?\", \"text-embedding-ada-002\"):\n    print(\"Embedding is in cache\")\n```\n\n#### `exists_by_key(key)`\n\nCheck if an embedding exists for the given Redis key.\n\n* **Parameters:**\n  **key** (*str*) – The full Redis key for the embedding.\n* **Returns:**\n  True if the embedding exists in the cache, False otherwise.\n* **Return type:**\n  bool\n\n```python\nif cache.exists_by_key(\"embedcache:1234567890abcdef\"):\n    print(\"Embedding is in cache\")\n```\n\n#### `expire(key, ttl=None)`\n\nSet or refresh the expiration time for a key in the cache.\n\n* **Parameters:**\n  * **key** (*str*) – The Redis key to set the expiration on.\n  * **ttl** (*Optional* *[* *int* *]* *,* *optional*) – The time-to-live in seconds. If None,\n    uses the default TTL configured for this cache instance.\n    Defaults to None.\n* **Return type:**\n  None\n\n#### `NOTE`\nIf neither the provided TTL nor the default TTL is set (both are None),\nthis method will have no effect.\n\n#### `get(text, model_name)`\n\nGet embedding by text and model name.\n\nRetrieves a cached embedding for the given text and model name.\nIf found, refreshes the TTL of the entry.\n\n* **Parameters:**\n  * **text** (*str*) – The text input that was embedded.\n  * **model_name** (*str*) – The name of the embedding model.\n* **Returns:**\n  Embedding cache entry or None if not found.\n* **Return type:**\n  Optional[Dict[str, Any]]\n\n```python\nembedding_data = cache.get(\n    text=\"What is machine learning?\",\n    model_name=\"text-embedding-ada-002\"\n)\n```\n\n#### `get_by_key(key)`\n\nGet embedding by its full Redis key.\n\nRetrieves a cached embedding for the given Redis key.\nIf found, refreshes the TTL of the entry.\n\n* **Parameters:**\n  **key** (*str*) – The full Redis key for the embedding.\n* **Returns:**\n  Embedding cache entry or None if not found.\n* **Return type:**\n  Optional[Dict[str, Any]]\n\n```python\nembedding_data = cache.get_by_key(\"embedcache:1234567890abcdef\")\n```\n\n#### `mdrop(texts, model_name)`\n\nRemove multiple embeddings from the cache by their texts and model name.\n\nEfficiently removes multiple embeddings in a single operation.\n\n* **Parameters:**\n  * **texts** (*List* *[* *str* *]*) – List of text inputs that were embedded.\n  * **model_name** (*str*) – The name of the embedding model.\n* **Return type:**\n  None\n\n```python\n# Remove multiple embeddings\ncache.mdrop(\n    texts=[\"What is machine learning?\", \"What is deep learning?\"],\n    model_name=\"text-embedding-ada-002\"\n)\n```\n\n#### `mdrop_by_keys(keys)`\n\nRemove multiple embeddings from the cache by their Redis keys.\n\nEfficiently removes multiple embeddings in a single operation.\n\n* **Parameters:**\n  **keys** (*List* *[* *str* *]*) – List of Redis keys to remove.\n* **Return type:**\n  None\n\n```python\n# Remove multiple embeddings\ncache.mdrop_by_keys([\"embedcache:key1\", \"embedcache:key2\"])\n```\n\n#### `mexists(texts, model_name)`\n\nCheck if multiple embeddings exist by their texts and model name.\n\nEfficiently checks existence of multiple embeddings in a single operation.\n\n* **Parameters:**\n  * **texts** (*List* *[* *str* *]*) – List of text inputs that were embedded.\n  * **model_name** (*str*) – The name of the embedding model.\n* **Returns:**\n  List of boolean values indicating whether each embedding exists.\n* **Return type:**\n  List[bool]\n\n```python\n# Check if multiple embeddings exist\nexists_results = cache.mexists(\n    texts=[\"What is machine learning?\", \"What is deep learning?\"],\n    model_name=\"text-embedding-ada-002\"\n)\n```\n\n#### `mexists_by_keys(keys)`\n\nCheck if multiple embeddings exist by their Redis keys.\n\nEfficiently checks existence of multiple keys in a single operation.\n\n* **Parameters:**\n  **keys** (*List* *[* *str* *]*) – List of Redis keys to check.\n* **Returns:**\n  List of boolean values indicating whether each key exists.\n  The order matches the input keys order.\n* **Return type:**\n  List[bool]\n\n```python\n# Check if multiple keys exist\nexists_results = cache.mexists_by_keys([\"embedcache:key1\", \"embedcache:key2\"])\n```\n\n#### `mget(texts, model_name)`\n\nGet multiple embeddings by their texts and model name.\n\nEfficiently retrieves multiple cached embeddings in a single operation.\nIf found, refreshes the TTL of each entry.\n\n* **Parameters:**\n  * **texts** (*List* *[* *str* *]*) – List of text inputs that were embedded.\n  * **model_name** (*str*) – The name of the embedding model.\n* **Returns:**\n  List of embedding cache entries or None for texts not found.\n* **Return type:**\n  List[Optional[Dict[str, Any]]]\n\n```python\n# Get multiple embeddings\nembedding_data = cache.mget(\n    texts=[\"What is machine learning?\", \"What is deep learning?\"],\n    model_name=\"text-embedding-ada-002\"\n)\n```\n\n#### `mget_by_keys(keys)`\n\nGet multiple embeddings by their Redis keys.\n\nEfficiently retrieves multiple cached embeddings in a single network roundtrip.\nIf found, refreshes the TTL of each entry.\n\n* **Parameters:**\n  **keys** (*List* *[* *str* *]*) – List of Redis keys to retrieve.\n* **Returns:**\n  List of embedding cache entries or None for keys not found.\n  The order matches the input keys order.\n* **Return type:**\n  List[Optional[Dict[str, Any]]]\n\n```python\n# Get multiple embeddings\nembedding_data = cache.mget_by_keys([\n    \"embedcache:key1\",\n    \"embedcache:key2\"\n])\n```\n\n#### `mset(items, ttl=None)`\n\nStore multiple embeddings in a batch operation.\n\nEach item in the input list should be a dictionary with the following fields:\n- ‘text’: The text input that was embedded\n- ‘model_name’: The name of the embedding model\n- ‘embedding’: The embedding vector\n- ‘metadata’: Optional metadata to store with the embedding\n\n* **Parameters:**\n  * **items** (*List* *[* *Dict* *[* *str* *,* *Any* *]* *]*) – List of dictionaries, each containing text, model_name, embedding, and optional metadata.\n  * **ttl** (*int* *|* *None*) – Optional TTL override for these entries.\n* **Returns:**\n  List of Redis keys where the embeddings were stored.\n* **Return type:**\n  List[str]\n\n```python\n# Store multiple embeddings\nkeys = cache.mset([\n    {\n        \"text\": \"What is ML?\",\n        \"model_name\": \"text-embedding-ada-002\",\n        \"embedding\": [0.1, 0.2, 0.3],\n        \"metadata\": {\"source\": \"user\"}\n    },\n    {\n        \"text\": \"What is AI?\",\n        \"model_name\": \"text-embedding-ada-002\",\n        \"embedding\": [0.4, 0.5, 0.6],\n        \"metadata\": {\"source\": \"docs\"}\n    }\n])\n```\n\n#### `set(text, model_name, embedding, metadata=None, ttl=None)`\n\nStore an embedding with its text and model name.\n\n* **Parameters:**\n  * **text** (*str*) – The text input that was embedded.\n  * **model_name** (*str*) – The name of the embedding model.\n  * **embedding** (*List* *[* *float* *]*) – The embedding vector to store.\n  * **metadata** (*Optional* *[* *Dict* *[* *str* *,* *Any* *]* *]*) – Optional metadata to store with the embedding.\n  * **ttl** (*Optional* *[* *int* *]*) – Optional TTL override for this specific entry.\n* **Returns:**\n  The Redis key where the embedding was stored.\n* **Return type:**\n  str\n\n```python\nkey = cache.set(\n    text=\"What is machine learning?\",\n    model_name=\"text-embedding-ada-002\",\n    embedding=[0.1, 0.2, 0.3, ...],\n    metadata={\"source\": \"user_query\"}\n)\n```\n\n#### `set_ttl(ttl=None)`\n\nSet the default TTL, in seconds, for entries in the cache.\n\n* **Parameters:**\n  **ttl** (*Optional* *[* *int* *]* *,* *optional*) – The optional time-to-live expiration\n  for the cache, in seconds.\n* **Raises:**\n  **ValueError** – If the time-to-live value is not an integer.\n* **Return type:**\n  None\n\n#### `property ttl: int | None`\n\nThe default TTL, in seconds, for entries in the cache.\n",
  "tags": [],
  "last_updated": "2026-04-01T08:10:08-05:00"
}

