index.d.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. export type CacheOptions<Key = unknown, Value = unknown> = {
  2. /** Maximum number of items the cache can hold. */
  3. max: number;
  4. /** Function called when an item is evicted from the cache. */
  5. onEviction?: (key: Key, value: Value) => unknown;
  6. };
  7. export declare const createLRU: <Key, Value>(options: CacheOptions<Key, Value>) => {
  8. /** Adds a key-value pair to the cache. Updates the value if the key already exists. */
  9. set(key: Key, value: Value): undefined;
  10. /** Retrieves the value for a given key and moves the key to the most recent position. */
  11. get(key: Key): Value | undefined;
  12. /** Retrieves the value for a given key without changing its position. */
  13. peek: (key: Key) => Value | undefined;
  14. /** Checks if a key exists in the cache. */
  15. has: (key: Key) => boolean;
  16. /** Iterates over all keys in the cache, from most recent to least recent. */
  17. keys(): IterableIterator<Key>;
  18. /** Iterates over all values in the cache, from most recent to least recent. */
  19. values(): IterableIterator<Value>;
  20. /** Iterates over `[key, value]` pairs in the cache, from most recent to least recent. */
  21. entries(): IterableIterator<[Key, Value]>;
  22. /** Iterates over each value-key pair in the cache, from most recent to least recent. */
  23. forEach: (callback: (value: Value, key: Key) => unknown) => undefined;
  24. /** Deletes a key-value pair from the cache. */
  25. delete(key: Key): boolean;
  26. /** Evicts the oldest item or the specified number of the oldest items from the cache. */
  27. evict: (number: number) => undefined;
  28. /** Clears all key-value pairs from the cache. */
  29. clear(): undefined;
  30. /** Resizes the cache to a new maximum size, evicting items if necessary. */
  31. resize: (newMax: number) => undefined;
  32. /** Returns the maximum number of items that can be stored in the cache. */
  33. readonly max: number;
  34. /** Returns the number of items currently stored in the cache. */
  35. readonly size: number;
  36. /** Returns the number of currently available slots in the cache before reaching the maximum size. */
  37. readonly available: number;
  38. };