https://github.com/bbc6bae9/cachekit
Swift memory & disk cache
https://github.com/bbc6bae9/cachekit
apple ios ipados macos swift swiftui visionos watchos
Last synced: 6 months ago
JSON representation
Swift memory & disk cache
- Host: GitHub
- URL: https://github.com/bbc6bae9/cachekit
- Owner: BBC6BAE9
- License: mit
- Created: 2024-02-21T07:08:12.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-09-03T11:52:11.000Z (10 months ago)
- Last Synced: 2025-10-14T03:27:00.330Z (9 months ago)
- Topics: apple, ios, ipados, macos, swift, swiftui, visionos, watchos
- Language: Swift
- Homepage: https://bbc6bae9.github.io/cachekit/documentation/cachekit/
- Size: 396 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 📦 CacheKit
Cache is a lightweight thread safe disk caching tool for Apple platform, commonly used to store data returned from network requests.
## Quick Start
1、Init cache
```Swift
import CacheKit
let cache = DiskCache<[YOUR_CODABLE_MODEL]>(filename:"YOUR_FILE_NAME", expirationInterval: 30 * 24 * 60 * 60)
```
2、Write cache to disk
```swift
cache.setValue(channels, forKey: keyString)
try? await cache.saveToDisk()
```
3、Load cache from disk
```Swift
cache.loadFromDisk()
```
4、Remove cache from disk
```swift
cache.removeValue(forKey: keyString)
try? await cache.saveToDisk()
```
5、More info
[https://bbc6bae9.github.io/cachekit/](https://bbc6bae9.github.io/CacheKit/documentation/cachekit/)
## Class Diagram
```mermaid
classDiagram
class Cache~V~ {
<>
+TimeInterval expirationInterval
+setValue(V?, forKey: String)
+value(forKey: String) V?
+removeValue(forKey: String)
+removeAllValues()
}
class NSCacheType~V~ {
<>
+NSCache~NSString, CacheEntry~V~~ cache
+KeysTracker~V~ keysTracker
+setValue(V?, forKey: String)
+value(forKey: String) V?
+removeValue(forKey: String)
+removeAllValues()
#entry(forKey: String) CacheEntry~V~?
#insert(CacheEntry~V~)
}
class InMemoryCache~V~ {
+init(expirationInterval: TimeInterval)
+TimeInterval expirationInterval
-NSCache~NSString, CacheEntry~V~~ cache
-KeysTracker~V~ keysTracker
}
class DiskCache~V~ {
+init(filename: String, expirationInterval: TimeInterval)
+String filename
+TimeInterval expirationInterval
+saveToDisk() throws
+loadFromDisk() throws
-NSCache~NSString, CacheEntry~V~~ cache
-KeysTracker~V~ keysTracker
-URL saveLocationURL
}
class CacheEntry~V~ {
+init(key: String, value: V, expiredTimestamp: Date)
+String key
+V value
+Date expiredTimestamp
+isCacheExpired(after: Date) Bool
}
class KeysTracker~V~ {
+Set~String~ keys
+cache(_:willEvictObject:) void
}
class NSCache {
<>
+setObject(AnyObject?, forKey: AnyObject)
+object(forKey: AnyObject) AnyObject?
+removeObject(forKey: AnyObject)
+removeAllObjects()
}
Cache <|.. NSCacheType
NSCacheType <|.. InMemoryCache
NSCacheType <|.. DiskCache
NSCacheType o-- CacheEntry
NSCacheType o-- KeysTracker
NSCacheType o-- NSCache
KeysTracker --|> NSCacheDelegate
CacheEntry ..|> Codable
```