Skip to content

Releases: 0xLeif/Cache

2.1.0

22 Jul 17:39
a535c68
Compare
Choose a tag to compare

What's Changed

PersistableCache

Breaking Change

  • Update PersistableCache to use mappings by @0xLeif in #13

Full Changelog: 2.0.0...2.1.0

2.0.0

07 Jul 20:00
d6d6325
Compare
Choose a tag to compare

What's Changed

Added

  • AnyCacheable type, enabling type erasure for any Cacheable object.
  • CacheInitializable protocol, introducing an initializer for Cacheable objects.
  • ComposableCache type, allowing for the composition of multiple caches into a single cache.
  • Logging property wrapper, providing a convenient way to cache and retrieve loggers.
  • Global.loggers cache, a required cache instance specifically designed for managing loggers.

Full Changelog: 1.6.0...2.0.0

1.6.0

29 Jun 03:12
a3f81b1
Compare
Choose a tag to compare

Adds

  • RequiredKeysCache: Introduced a new cache type, RequiredKeysCache, that ensures all specified keys are present in the cache at all times. This cache is useful when you have a defined set of keys that must always be present.

  • Global Caches: Added three global cache instances to simplify usage:

    • cache: A global cache instance for generic caching needs.
    • images: A global cache instance specifically designed for storing and retrieving images.
    • dependencies: A global cache instance for storing and retrieving dependencies.
  • Property Wrappers: Added three new property wrappers for easy cache integration:

  • Cached: A property wrapper that automatically retrieves and caches the value from the associated cache.

  • OptionallyCached: A property wrapper that retrieves the value from the associated cache if available, or falls back to the default value.

  • Resolved: A property wrapper that resolves the value from the associated cache, throwing an error if the value is missing or cannot be resolved.

With these additions, the Cache library becomes even more versatile and convenient to use. Happy caching!

Test Coverage for Windows and Linux

  • Added tests for Windows platform to ensure compatibility and functionality on the Windows operating system.

  • Added tests for Linux platform to ensure compatibility and functionality on the Linux operating system.


What's Changed

Full Changelog: 1.5.0...1.6.0

1.5.0

10 Jun 23:39
5a797f4
Compare
Choose a tag to compare

LRUCache

LRUCache is a key-value store that uses the Least Recently Used (LRU) algorithm to evict items when the cache capacity is exceeded. This implementation is written in Swift and can be used in any iOS, macOS, or tvOS project. The cache contents are automatically loaded from the initial values dictionary when initialized.

The LRUCache is implemented as a subclass of the Cache class. You can use its capacity property to specify the maximum number of key-value pairs that the cache can hold. When the cache capacity is exceeded, the least recently used item is automatically evicted from the cache.


What's Changed

Full Changelog: 1.4.0...1.5.0

1.4.0

10 Jun 21:22
e8bb8f8
Compare
Choose a tag to compare

PersistableCache

The PersistableCache class is a cache that stores its contents persistently on disk using a JSON file. Use it to create a cache that persists its contents between application launches. The cache contents are automatically loaded from disk when initialized, and can be saved manually whenever required.

To use PersistableCache, make sure that the specified key type conforms to both RawRepresentable and Hashable protocols. The RawValue of Key must be a String type.

Here's an example of creating a cache, setting a value, and saving it to disk:

let cache = PersistableCache<String, Double>()

cache["pi"] = Double.pi

do {
    try cache.save()
} catch {
    print("Failed to save cache: \(error)")
}

You can also load a previously saved cache from disk:

let cache = PersistableCache<String, Double>()

let pi = cache["pi"] // pi == Double.pi

Remember that the save() function may throw errors if the encoder fails to serialize the cache to JSON or the disk write operation fails. Make sure to handle the errors appropriately.


What's Changed

Full Changelog: 1.3.0...1.4.0

1.3.0

10 Jun 04:36
538f770
Compare
Choose a tag to compare

What's Changed

  • Add dictionary extensions and Cacheable by @0xLeif in #4

Full Changelog: 1.2.0...1.3.0

1.2.0

10 Jun 01:21
a1ad092
Compare
Choose a tag to compare

What's Changed

Full Changelog: 1.1.0...1.2.0

1.1.0

09 Jun 22:15
6ddb9ec
Compare
Choose a tag to compare

ExpiringCache

The ExpiringCache class is a cache that retains and returns objects for a specific duration set by the ExpirationDuration enumeration. Objects stored in the cache are automatically removed when their expiration duration has passed.

Usage

// Create an instance of the cache with a duration of 5 minutes
let cache = ExpiringCache<String, Int>(duration: .minutes(5))

// Store a value in the cache with a key
cache["Answer"] = 42

// Retrieve a value from the cache using its key
if let answer = cache["Answer"] {
    print("The answer is \(answer)")
}

Expiration Duration

The expiration duration of the cache can be set with the ExpirationDuration enumeration, which has three cases: seconds, minutes, and hours. Each case takes a single UInt argument to represent the duration of that time unit.


What's Changed

Full Changelog: 1.0.1...1.1.0

1.0.1

09 Jun 18:29
619fdbf
Compare
Choose a tag to compare

Cache

A simple, lightweight caching library for Swift.

What is Cache?

Cache is a Swift library for caching arbitrary data types in memory. It provides a simple and intuitive API for storing, retrieving, and removing objects from the cache.

Features

  • Generic value type
  • Supports JSON serialization and deserialization
  • Flexible caching, allowing for multiple Cache objects with different configurations
  • Thread-safe implementation

Installation

Swift Package Manager (SPM)

Add the following line to your Package.swift file in the dependencies array:

dependencies: [
    .package(url: "https://github.com/0xLeif/Cache.git", from: "1.0.0")
]

Usage

Basic Usage

First, import the Cache module:

import Cache

Create a cache instance with a generic key-value pair:

let cache = Cache<CacheKey, String>()

Add values to the cache using a key-value syntax:

cache[.text] = "Hello, World!"

Retrieve values using the same key-value syntax:

let cachedValue = cache[.text]

Multiple Cache Objects

You can create multiple Cache objects by specifying a different type of key-value pair:

let cache1 = Cache<CacheKey, String>()
let imageCache = Cache<URL, UIImage>()

Using JSON

You can use JSON to parse and serialize JSON data in the cache:

let json: JSON<CacheKey> = JSON(data: jsonData)

Removing Values

You can remove values from the cache using the remove method:

cache.remove(.text)

You can also just set the value to nil using the subscripts

cache[.text] = nil

Advanced Usage

You can use Cache as an observed object:

struct ExampleView: View {
    enum Key {
        case title
    }

    @ObservedObject var cache = Cache<Key, String>()

    var body: some View {
        TextField(
            "Cache Title",
            text: $cache[.title, default: ""]
        )
    }
}

Cacheable Functions

Cacheable protocol defines the following functions which can be used to work with the Cache or JSON.

allValues

allValues property returns a dictionary containing all the key-value pairs stored in the cache.

var allValues: [Key: Value] { get }

init

The init function initializes the cache instance with an optional dictionary of key-value pairs.

init(initialValues: [Key: Value])

get

The get function retrieves the value of the specified key and casts it to a given output type (if possible).

func get<Output>(_ key: Key, as: Output.Type) -> Output?

Alternatively, calling get with only the key returns the value casted to the default Value type.

func get(_ key: Key) -> Value?

resolve

The resolve function retrieves the value of the specified key and casts it to a given output type, but throws an error if the specified key is missing or if the value cannot be casted to the given output type.

func resolve<Output>(_ key: Key, as: Output.Type) throws -> Output

Alternatively, calling resolve with only the key casts the value to the default Value type.

func resolve(_ key: Key) throws -> Value

set

The set method sets the specified value for the specified key in the cache.

func set(value: Value, forKey key: Key)

remove

The remove method removes the value of the specified key from the cache.

func remove(_ key: Key)

contains

The contains method returns a Boolean indicating whether the cache contains the specified key.

func contains(_ key: Key) -> Bool

require

The require function ensures that the cache contains the specified key or keys, or else it throws an error.

func require(_ key: Key) throws -> Self
func require(keys: Set<Key>) throws -> Self

values

The values function returns a dictionary containing only the key-value pairs where the value is of the specified output type.

func values<Output>(ofType: Output.Type) -> [Key: Output]

The default value for ofType parameter is Value.

Contributing

If you have improvements or issues, feel free to open an issue or pull request!

License

Cache is released under the MIT License. See LICENSE for details.


Full Changelog: 1.0.0...1.0.1

1.0.0

09 Jun 18:23
Compare
Choose a tag to compare

Cache

A simple, lightweight caching library for Swift.

What is Cache?

Cache is a Swift library for caching arbitrary data types in memory. It provides a simple and intuitive API for storing, retrieving, and removing objects from the cache.

Features

  • Generic value type
  • Supports JSON serialization and deserialization
  • Flexible caching, allowing for multiple Cache objects with different configurations
  • Thread-safe implementation

Installation

Swift Package Manager (SPM)

Add the following line to your Package.swift file in the dependencies array:

dependencies: [
    .package(url: "https://github.com/0xLeif/Cache.git", from: "1.0.0")
]

Usage

Basic Usage

First, import the Cache module:

import Cache

Create a cache instance with a generic key-value pair:

let cache = Cache<CacheKey, String>()

Add values to the cache using a key-value syntax:

cache[.text] = "Hello, World!"

Retrieve values using the same key-value syntax:

let cachedValue = cache[.text]

Multiple Cache Objects

You can create multiple Cache objects by specifying a different type of key-value pair:

let cache1 = Cache<CacheKey, String>()
let imageCache = Cache<URL, UIImage>()

Using JSON

You can use JSON to parse and serialize JSON data in the cache:

let json: JSON<CacheKey> = JSON(data: jsonData)

Removing Values

You can remove values from the cache using the remove method:

cache.remove(.text)

You can also just set the value to nil using the subscripts

cache[.text] = nil

Advanced Usage

You can use Cache as an observed object:

struct ExampleView: View {
    enum Key {
        case title
    }

    @ObservedObject var cache = Cache<Key, String>()

    var body: some View {
        TextField(
            "Cache Title",
            text: $cache[.title, default: ""]
        )
    }
}

Cacheable Functions

Cacheable protocol defines the following functions which can be used to work with the Cache or JSON.

allValues

allValues property returns a dictionary containing all the key-value pairs stored in the cache.

var allValues: [Key: Value] { get }

init

The init function initializes the cache instance with an optional dictionary of key-value pairs.

init(initialValues: [Key: Value])

get

The get function retrieves the value of the specified key and casts it to a given output type (if possible).

func get<Output>(_ key: Key, as: Output.Type) -> Output?

Alternatively, calling get with only the key returns the value casted to the default Value type.

func get(_ key: Key) -> Value?

resolve

The resolve function retrieves the value of the specified key and casts it to a given output type, but throws an error if the specified key is missing or if the value cannot be casted to the given output type.

func resolve<Output>(_ key: Key, as: Output.Type) throws -> Output

Alternatively, calling resolve with only the key casts the value to the default Value type.

func resolve(_ key: Key) throws -> Value

set

The set method sets the specified value for the specified key in the cache.

func set(value: Value, forKey key: Key)

remove

The remove method removes the value of the specified key from the cache.

func remove(_ key: Key)

contains

The contains method returns a Boolean indicating whether the cache contains the specified key.

func contains(_ key: Key) -> Bool

require

The require function ensures that the cache contains the specified key or keys, or else it throws an error.

func require(_ key: Key) throws -> Self
func require(keys: Set<Key>) throws -> Self

values

The values function returns a dictionary containing only the key-value pairs where the value is of the specified output type.

func values<Output>(ofType: Output.Type) -> [Key: Output]

The default value for ofType parameter is Value.

Contributing

If you have improvements or issues, feel free to open an issue or pull request!

License

Cache is released under the MIT License. See LICENSE for details.


Full Changelog: https://github.com/0xLeif/Cache/commits/1.0.0