https://github.com/modernistik/memoryjar
Fast, efficient, and thread-safe API LRU caching for Swift
https://github.com/modernistik/memoryjar
api cache ios swift
Last synced: 13 days ago
JSON representation
Fast, efficient, and thread-safe API LRU caching for Swift
- Host: GitHub
- URL: https://github.com/modernistik/memoryjar
- Owner: modernistik
- License: mit
- Created: 2019-07-27T18:33:09.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2022-06-26T19:50:15.000Z (almost 3 years ago)
- Last Synced: 2025-04-09T17:20:34.626Z (13 days ago)
- Topics: api, cache, ios, swift
- Language: Swift
- Homepage:
- Size: 28.3 KB
- Stars: 4
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MemoryJar
MemoryJar is a fast and efficient and thread-safe persistent string caching library that includes capacity management (LRU) and support for age expiration. It utilizes both in-memory and disk storage, supporting asynchronous writes for speed. This library was inspired by the caching mechanism on the Parse iOS SDK.This caching library is most useful when building a caching system for managing a REST API.
[](https://travis-ci.org/modernistik/MemoryJar)
[](https://cocoapods.org/pods/MemoryJar)
[](https://cocoapods.org/pods/MemoryJar)
[](https://cocoapods.org/pods/MemoryJar)## Installation
To install it, simply add the following line to your Podfile:
```ruby
pod "MemoryJar"
```## Usage
```swift
import MemoryJar// use shared, or create your own with MemoryJar()
let cache = MemoryJar.shared// Simple
cache["company"] = "Modernistik"
// retrieve (no expiration)
let company = cache["company"]// Some API response
let json = """
{
"name" : "Anthony Persaud",
"id" : 7,
"company" : {
"name" : "Modernistik",
"location": "San Diego, CA"
}
}
"""let cacheKey = "https://some.api/?id=7"
// set the value
cache.set(value: json, forKey: cacheKey)// fetch value only if it is not older than 1 day.
if let result = cache.get(forKey: cacheKey, maxAge: 86400) {
print(result)
}// deletes all cache objects
cache.removeAllObjects()
```## Todo
* Support clearing specific url paths matching a pattern.## Author
Anthony Persaud, https://www.modernistik.com
## License
MemoryJar is available under the MIT license. See the LICENSE file for more info.