# LRU Memory Cache Library in Rust

> A simple crate that provides an in-memory cache implementation with a Least Recently Used (LRU) eviction strategy.

- URL: https://zeljic.com/projects/in-memory-cache/
- Author: Đorđe Zeljić
- Published: 2023-10-08
- Updated: 2023-10-09
- Tags: rust, cache, lru, memory, library

**in-memory-cache** is a straightforward, yet powerful crate that delivers an easy-to-use in-memory cache solution with a Least Recently Used (LRU) eviction policy.
This crate facilitates the efficient storage of key-value pairs, providing automatic eviction of the least recently accessed entries when the cache reaches its predefined capacity.

## Features

- Easy-to-use API for adding and retrieving items from the cache.
- Supports two types of limits: capacity and size.
- Supports limiting cache size based on the number of entries or the total size in bytes.
- Evicts the oldest entries when the cache reaches its limit.
- Error handling for content that exceeds the cache's size limit.

## Usage

To use the `in-memory-cache` crate, add the following dependency to your `Cargo.toml` file:

```toml
[dependencies.in-memory-cache]
version = "0.4"
git = "https://github.com/zeljic/in-memory-cache.git"
```

### Example

Here is an example demonstrating how to use the `in-memory-cache` crate:

```rust
use in_memory_cache::{Cache, Entry};

fn main() {
    // Create a cache with a capacity of 5 items
    let mut cache = Cache::with_capacity(5);

    // Add entries to the cache
    cache.add("key1", "value1").unwrap();
    cache.add("key2", "value2").unwrap();
    cache.add("key3", "value3").unwrap();
    cache.add("key4", "value4").unwrap();
    cache.add("key5", "value5").unwrap();

    // Retrieve an entry from the cache
    if let Some(entry) = cache.get("key1") {
        println!("Entry found: {:?}", entry);
    }

    // Add a new entry, evicting the oldest entry from the cache
    cache.add("key6", "value6").unwrap();

    // Clear the cache
    cache.clear();
}
```

In this example, we create a cache with a capacity of 5 items using `Cache::with_capacity()`. We use the `add()` method to add entries to the cache, and the `get()` method to retrieve entries from the cache.

If the cache reaches its capacity, the oldest entry will be evicted when a new entry is added. The `clear()` method can be used to remove all entries from the cache.

## Further customization

The `Cache` struct provides additional methods to create a cache with a size-based limit and methods to retrieve entries as `bytes::Bytes`. You can choose the appropriate method based on your specific use case.

## Error Handling

The `add()` method returns a `Result<(), Box<dyn std::error::Error>>`, allowing you to handle errors that occur when adding entries to the cache. If the content exceeds the cache's size limit, an `Error` will be returned.

## License

This crate is licensed under the [MIT License](https://opensource.org/licenses/MIT). Please refer to the [LICENSE](https://github.com/zeljic/in-memory-cache/blob/master/LICENSE) file for detailed licensing information.

<div class="flex flex-row gap-4 w-full">
	<img src="https://img.shields.io/github/license/zeljic/in-memory-cache" alt="License"/>
	<img src="https://img.shields.io/github/last-commit/zeljic/in-memory-cache" alt="Last commit"/>
</div>
