file

class litestar.stores.file.FileStore[原始碼]

基底類別:NamespacedStore

File based, thread and process safe, asynchronous key/value store.

備註

To ensure arbitrary keys can safely be stored on any file system without potentially causing issues due to path separators or collisions, they are hashed with BLAKE2 before being interpreted as a file path. This means that the cache key becomes opaque inside the store, and a key does not translate to a file with that name on the file system.

__init__(path: PathLike[str], *, create_directories: bool = False) None[原始碼]

初始化 FileStorage

參數:
  • path -- Path to store data under

  • create_directories --

    Create the directories in path if they don't exist Default: False

    在 2.9.0 版被加入.

path

file path

create_directories

flag to create directories in path

with_namespace(namespace: str) FileStore[原始碼]

Return a new instance of FileStore, using a sub-path of the current store's path.

async set(key: str, value: str | bytes, expires_in: int | timedelta | None = None) None[原始碼]

Set a value.

參數:
  • key -- Key to associate the value with

  • value -- Value to store

  • expires_in -- Time in seconds before the key is considered expired

回傳:

None

async get(key: str, renew_for: int | timedelta | None = None) bytes | None[原始碼]

Get a value.

參數:
  • key -- Key associated with the value

  • renew_for -- If given and the value had an initial expiry time set, renew the expiry time for renew_for seconds. If the value has not been set with an expiry time this is a no-op

回傳:

The value associated with key if it exists and is not expired, else None

async delete(key: str) None[原始碼]

Delete a value.

If no such key exists, this is a no-op.

參數:

key -- Key of the value to delete

async delete_all() None[原始碼]

Delete all stored values.

備註

This deletes and recreates FileStore.path

async delete_expired() None[原始碼]

刪除過期的項目。

Since expired items are normally only cleared on access (i.e. when calling get()), this method should be called in regular intervals to free disk space.

async exists(key: str) bool[原始碼]

Check if a given key exists.

async expires_in(key: str) int | None[原始碼]

Get the time in seconds key expires in. If no such key exists or no expiry time was set, return None.