Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mz2/weightedlrucache

An LRU cache in Swift, with optional support for weighted values such that values are evicted from the tail until a total weight parameter is satisfied.
https://github.com/mz2/weightedlrucache

Last synced: 25 days ago
JSON representation

An LRU cache in Swift, with optional support for weighted values such that values are evicted from the tail until a total weight parameter is satisfied.

Awesome Lists containing this project

README

        

# WeightedLRUCache [![Swift Package Manager compatible](https://img.shields.io/badge/Swift%20Package%20Manager-compatible-brightgreen.svg)](https://github.com/apple/swift-package-manager) [![Build](https://github.com/mz2/WeightedLRUCache/actions/workflows/build.yml/badge.svg)](https://github.com/mz2/WeightedLRUCache/actions/workflows/build.yml) ![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)
An LRU cache in Swift, with optional support for weighted values such that values are evicted from the tail until a total weight parameter is satisfied.

## Installation

Add WeightedLRUCache to your Swift package as a dependency by adding the following to your Package.swift file in the dependencies array:

```swift
.package(url: "https://github.com/mz2/WeightedLRUCache.git", from: "")
```

If you are using Xcode 11 or newer, you can add WeightedLRUCache by entering the URL to the repository via the File menu:

```
File > Swift Packages > Add Package Dependency...
```

## Usage

Values inserted into a `WeightedLRUCache` must conform to the protocol `Weighted` (which simply requires a readonly property `weight: UInt` on the conforming type).
An example implementation `WeightedValue` is provided with the library.

To create a cache with no constrained item count, and a max weight constraint of `10`, do the following:
```swift
var cache = WeightedLRUCache>(maxCount: .max, maxWeight: 10)
cache.didEvict = { key, value in
print("Dropped \(key) : \(value)")
}
```

Note above the use of `Int.max` as the max count -- here only a weight constraint was given (both item count and total weight can be constrained). Let's add two more items in there:

```swift
cache["a"] = WeightedValue(weight: 5, value: "foo")
cache["b"] = WeightedValue(weight: 5, value: "bar")

// Upon this being inserted, value with key "a" above is dropped,
// and the callback passed to the cache initializer is called for the first inserted value.
cache["c"] = WeightedValue(weight: 1, value: "baz")
```

A cache created as above behaves such that...
- the first inserted value (with key `"a"`) will be dropped after the next two have been inserted (since the 3rd insertion brings the total weight to `11`, i.e. above the maximum of `10`).
- the callback passed into the initializer is called (synchronously, in the call stack that results from the cache insertion of key `"c"` below).

Beside a subscript based interface for key-based access, `keys` and `values` arrays available on `WeightedLRUCache` to return values in the order in which they were accessed by their key.
Given the example above, `keys` for example returns the array `["c", "b", "a"]`.

For more usage examples, see the project's test suite.

## License

WeightedLRUCache is available under the MIT license.