UNPKG

tauri-plugin-cache-api

Version:

Comprehensive disk and memory caching solution for Tauri applications. Features dynamic TTL management, intelligent data compression, automatic cleanup, and statistics monitoring. Delivers high-performance data access, optimized storage, and improved user

125 lines (122 loc) 3.44 kB
import { invoke } from '@tauri-apps/api/core'; /** * Supported compression methods */ var CompressionMethod; (function (CompressionMethod) { /** * Zlib compression (default, balanced speed/ratio) */ CompressionMethod["Zlib"] = "zlib"; /** * LZMA2 compression (better compression ratio, slower) */ CompressionMethod["Lzma2"] = "lzma2"; })(CompressionMethod || (CompressionMethod = {})); /** * Sets an item in the cache with optional TTL and compression * @param key The key to store the value under * @param value The value to store * @param options Options for setting the cache item * @returns A promise that resolves when the operation is complete * @example * ```typescript * // Set an item with no expiration * await cache.set('user', { name: 'John', age: 30 }); * * // Set an item that expires in 60 seconds * await cache.set('token', 'abc123', { ttl: 60 }); * * // Set an item with compression enabled * await cache.set('largeData', largeObject, { compress: true }); * * // Set an item with both TTL and compression * await cache.set('temporaryData', data, { ttl: 300, compress: true }); * * // Set an item with LZMA2 compression for better compression ratio * await cache.set('largeTextData', largeText, { compress: true, compressionMethod: CompressionMethod.Lzma2 }); * * // Set an item with Zlib compression for faster compression * await cache.set('mediumData', mediumObject, { compress: true, compressionMethod: CompressionMethod.Zlib }); * ``` */ async function set(key, value, options) { await invoke('plugin:cache|set', { key, value, options, }); } /** * Gets an item from the cache * @param key The key to retrieve * @returns The stored value or null if not found or expired * @example * ```typescript * const user = await cache.get<User>('user'); * if (user) { * console.log(user.name); // 'John' * } * ``` */ async function get(key) { const result = await invoke('plugin:cache|get', { key, }); return result === undefined ? null : result; } /** * Checks if an item exists in the cache and is not expired * @param key The key to check * @returns True if the item exists and is not expired * @example * ```typescript * if (await cache.has('user')) { * // User exists in cache * } * ``` */ async function has(key) { const response = await invoke('plugin:cache|has', { key, }); return response.value; } /** * Removes an item from the cache * @param key The key to remove * @returns A promise that resolves when the operation is complete * @example * ```typescript * await cache.remove('user'); * ``` */ async function remove(key) { await invoke('plugin:cache|remove', { key, }); } /** * Clears all items from the cache * @returns A promise that resolves when the operation is complete * @example * ```typescript * await cache.clear(); * ``` */ async function clear() { await invoke('plugin:cache|clear'); } /** * Gets statistics about the cache * @returns Cache statistics including the number of active and total items * @example * ```typescript * const stats = await cache.stats(); * console.log(`Cache has ${stats.totalSize} items (${stats.activeSize} active)`); * ``` */ async function stats() { return await invoke('plugin:cache|stats'); } export { CompressionMethod, clear, get, has, remove, set, stats };