Comments (5)
Hi @serendipityapps! Thanks for reaching out.
You’re right, out-of-the-box MemoryStorage
does not react to any system warnings. Shallows tries to be a very unopinionated about the logic of your app, so it doesn’t do anything behind your back. Instead, Shallows was designed to be able to achieve any kind of behavior easily.
First: technically, you can manually clear a MemoryStorage
if you have a direct reference to it. There’s a mutable storage
property which you can use:
let memoryCache = MemoryStorage<Filename, Data>()
let combined = memoryCache.combined(with: diskStorage) // Storage<Filename, Data>
// ...
memoryCache.storage = [:]
Second: here’s how I would make a «memory warnings aware» storage in Shallows:
public final class MemoryWarningsAwareMemoryStorage<Key : Hashable, Value> : StorageProtocol {
public let memoryStorage: MemoryStorage<Key, Value>
public init(memoryStorage: MemoryStorage<Key, Value>) {
self.memoryStorage = memoryStorage
self.startListening()
}
private func startListening() {
NotificationCenter.default.addObserver(forName: .UIApplicationDidReceiveMemoryWarning,
object: self,
queue: nil) { (_) in
self.memoryStorage.storage = [:]
}
}
deinit {
NotificationCenter.default.removeObserver(self)
}
public func retrieve(forKey key: Key, completion: @escaping (Result<Value>) -> ()) {
memoryStorage.retrieve(forKey: key, completion: completion)
}
public func set(_ value: Value, forKey key: Key, completion: @escaping (Result<Void>) -> ()) {
memoryStorage.set(value, forKey: key, completion: completion)
}
}
extension MemoryStorage {
public func lowMemoryAware() -> Storage<Key, Value> {
return MemoryWarningsAwareMemoryStorage(memoryStorage: self).asStorage()
}
}
And then:
let memoryStorage = MemoryStorage<Filename, Data>().lowMemoryAware()
let combined = memoryStorage.combined(with: diskStorage)
Hope this helps. Let me know!
from shallows.
Hey Oleg,
Thanks for quick reply and great to see your method of clearing storage was so simple
The solution provided for memory aware memory storage also looks very useful. So much so, I wonder if this or a variant should be included in source anyway. I would think that this behaviour would probably be the default behaviour (expected?) and maybe include a flag to NOT use auto clearance if required by client?
from shallows.
This is a very valid point. Asking for such widely-needed feature to be included by default is, of course, reasonable. But there was a design decision when making Shallows to keep it very small and to the point. I don’t want to make assumptions about how developers will structure their logic. For example, if someone does not have a disk-backed solution (and the MemoryStorage
is a single source of truth), auto clearance can be an almost impossible-to-detect bug.
from shallows.
Fair enough, thanks again.
from shallows.
You’re welcome! Ping me anytime if you'll have any questions
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
- Cache Expiration HOT 5
- Issue with combined cache creation and DiskFolderStorage HOT 2
- 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.