FragmentedKeys is a Swift library designed to streamline the management of cache invalidation by leveraging tag-value pair versioning. It's ideal for applications requiring sophisticated cache control mechanisms, such as web and mobile apps with dynamic content. The library supports multiple storage backends including NSCache
, Disk, and SQL.
- Tag-Based Versioning: Efficiently manage cache invalidation using tag-instance versioning.
- Flexible Storage Options: Choose from various persistence stores like
NSCache
, Disk, or SQL through thePersistenceManager
. - Advanced Tag Types: Utilize a variety of tag types, including
StaticTag
,LocaleTag
,AccountTag
,CacheTag
, andIntervalTag
for diverse caching strategies. - Asynchronous Operations: Fetch and populate cache entries asynchronously for optimal performance.
- Thread-Safe Implementation: Ensure safe use across multiple threads with built-in thread safety.
To integrate FragmentedKeys into your Swift project using SPM, add the following to your Package.swift
file:
dependencies: [
.package(url: "https://github.com/yourusername/FragmentedKeys.git", .upToNextMajor(from: "1.0.0"))
]
Replace https://github.com/yourusername/FragmentedKeys.git
with the actual URL of your library.
First, import FragmentedKeys in your Swift file.
import FragmentedKeys
Create tags for cache management:
let userTag = CacheTag(name: "User", subject: userID)
let globalTag = StaticTag("GlobalSettings")
Instantiate a KeyRing
with the desired tags and a persistence manager:
let keyRing = FragmentedKeyManager.shared.declareKeyRing(withTags: [userTag, globalTag])
Use the KeyRing
to fetch and store data in the cache:
let userData = keyRing.fetch(default: { self.loadUserData() }, persist: true)
Invalidate cache related to a specific tag:
userTag.incrementVersion()
For more advanced scenarios, like using interval-based tags or subscribing to specific changes, refer to the detailed documentation provided in the library.
Contributions are welcome. Please read the contributing guide and code of conduct on our repository.
FragmentedKeys is released under the MIT License.