Giter Site home page Giter Site logo

alvincrisuy / flatcache Goto Github PK

View Code? Open in Web Editor NEW

This project forked from githawkapp/flatcache

0.0 2.0 0.0 28 KB

Implementation of Soroush Khanlou's Flat Cache.

Home Page: http://khanlou.com/2017/10/the-flat-cache/

License: MIT License

Ruby 5.17% Objective-C 4.85% Swift 89.98%

flatcache's Introduction

FlatCache

Store any object in a flat cache by String id and notify listeners when changes occur.

This is an implementation of Soroush Khanlou's The Flat Cache blog post used in GitHawk.

Installation

Just add FlatCache to your Podfile and pod install. Done!

pod 'FlatCache'

Usage

Add the Cachable protocol to models that you want to store in the cache. Conform to the protocol by returning a String identifier used for lookup.

struct User {
  let name: String
}

extension User: Cachable {
  var id: String {
    return name
  }
}

Don't worry about id collisions between objects. The cache "namespaces" different models by type.

Now just create a FlatCache object and start reading & writing with it.

let cache = FlatCache()
let user = User(name: "ryan")
cache.set(value: user)
if let cached = cache.get(id: user.id) as User? {
  print(cached.name) // "ryan"
}

FlatCache uses the type information with the get(id:) function to lookup the appropriate object. You must type the result somehow.

Listeners

One of the strengths of FlatCache is adding "listeners" to the cache to be notified when an object is changed. This powerful tool let's multiple systems respond to object changes.

let cache = FlatCache()
let user = User(name: "ryan")
let listener = MyUserListener()
cache.add(listener: listener, value: user)

Now whenever user is set again in the cache, MyUserListener will be notified with the following function:

func flatCacheDidUpdate(cache: FlatCache, update: FlatCache.Update) {
  switch update {
  case .item(let item):
    // just a single object updated
  case .list:
    // a list of subscribed objects updated
  }
}

FlatCache coalesces so only a single event is delivered when something changes, no matter if an update has just a single object or hundreds.

Acknowledgements

flatcache's People

Contributors

rnystrom avatar rizwankce avatar jdisho avatar nanoxd avatar

Watchers

Alvin Cris Uy avatar  avatar

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.