Comments (5)
Hi @nmccann! Thanks for your thoughts, I really appreciate it.
I’m having a day off now, but I’ll get back at Sunday to discuss this very interesting question at length. So stay tuned!
from shallows.
Okay, I’m here now.
Cache expiration is, of course, a very useful and reasonable requirements for many apps, but it’s implementation is way too opinionated to be included in the library — I view Shallows as a toolbox that doesn’t perform any kind of dark magic under the hood.
As for the implementation itself, I think it can be done pretty easily. Here’s what I came up with:
final class LeastRecentlyUsedStorage<Key : Hashable, Value> : StorageProtocol {
let storage: Storage<Key, Value>
let metadata: Storage<Void, [Key : Date]>
init(storage: Storage<Key, Value>, metadata: Storage<Void, [Key : Date]>) {
self.storage = storage
self.metadata = metadata
}
func retrieve(forKey key: Key, completion: @escaping (Result<Value>) -> ()) {
storage.retrieve(forKey: key) { (result) in
self.metadata.update({ $0[key] = Date() })
completion(result)
}
}
func set(_ value: Value, forKey key: Key, completion: @escaping (Result<Void>) -> ()) {
storage.set(value, forKey: key) { (result) in
self.metadata.update({ $0[key] = Date() })
completion(result)
}
}
}
As you see, it showcases what I love about Shallows — you actually don’t know pretty much anything about where or how the values are stored, you only declare the logic of LRU component. It also doesn’t perform any kind of cache clearance itself, but you can write a simple extension to simplify this process:
extension LeastRecentlyUsedStorage {
func keys(usedEarlierThan date: Date, completion: @escaping (Result<[Key]>) -> ()) {
metadata.asReadOnlyStorage()
.mapValues(to: [Key].self, { meta in
let filtered = meta.filter({ key, value in value < date })
return Array(filtered.keys)
})
.retrieve(completion: completion)
}
}
And do any kind of logic needed outside the LeastRecentlyUsedStorage
object.
Ping me if you have any questions!
from shallows.
That is a very nice solution! And yes, I agree that it would be difficult to come up with something that would handle all possible cases - better to give users the equipment needed to implement it themselves. While a specialized solution may give better performance/more compact storage (for instance, by storing the access date in extended file attributes), it would be so specialized that it might be impossible to mix with any of the other tools in your toolbox analogy.
One question I do have: Is there any particular reason why your metadata
property is of type Storage<Void, [Key : Date]>
rather than Storage<Key, Date>
?
from shallows.
...it would be so specialized that it might be impossible to mix with any of the other tools in your toolbox analogy
That’s very true! That’s a tricky balance for sure, but I mostly try to keep my structures abstract.
Speaking of metadata
— well, the only reason to have that Storage<Void, [Key : Date]>
is to have the ability to iterate over keys.
from shallows.
Ah I see, because StorageProtocol
only has methods for getting/setting one key at a time. Maybe that would be something useful for the toolbox - an IterableStorageProtocol
. That said, providing implementations for all of the existing storage types would add a lot of extra surface area to the codebase when you could just use the workaround of Storage<Void, [Key : Date]>
.
In any case, I think we can close this issue. Thank you for your help!
from shallows.
Related Issues (15)
- 3-layer composition? HOT 14
- Purge from memoryCache (while leaving in composed diskCache)? HOT 10
- Memory cache returns nothing HOT 4
- Archiving fails in Swift5 HOT 13
- Xcode 11.4.1, Swift 5.2.2 Abort trap: 6 points to Shallows HOT 1
- How to handle success in StorageProtocol set(value:forKey:completion:) HOT 13
- No mechanism for deleting from disk storage HOT 1
- Base64 encoding isn't great for filenames HOT 2
- Clear object from MemoryStorage? HOT 3
- Cocoapods? HOT 5
- Issue with combined cache creation and DiskFolderStorage HOT 2
- Memory limitations and responding to warnings with combined cache? HOT 5
- Clearer Error handling HOT 3
- update(forKey: ) is not thread safe HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from shallows.