Giter Site home page Giter Site logo

Comments (5)

dreymonde avatar dreymonde commented on July 22, 2024

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.

serendipityapps avatar serendipityapps commented on July 22, 2024

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.

dreymonde avatar dreymonde commented on July 22, 2024

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.

serendipityapps avatar serendipityapps commented on July 22, 2024

Fair enough, thanks again. 👍

from shallows.

dreymonde avatar dreymonde commented on July 22, 2024

You’re welcome! Ping me anytime if you'll have any questions

from shallows.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.