Coder Social home page Coder Social logo

rxobservablecache's Introduction

Swift 5.0 | RU_Readme

RxObservableCache

Allows you to cache data for Observable

Cocoapods:

pod 'RxObservableCache', '~> 2.0.0'

How to use

1. Create object CacheAssociation<T>

This object is used to access the cache, T is the data type for which the cache is created. Through CacheAssociation, a connection is established with the cache and rules are set for reading / writing to the cache.

CacheAssociation.init(cacheIdentifier: CacheIdentifier, rule: CacheRule, groupId: CacheGroupId? = nil)
  • CacheIdentifier - unique identifier for cached data
  • CacheRule - cache interaction rule
public enum CacheRule {
    case readOnly(ExpiredAfterSeconds) 
    case readWrite(ExpiredAfterSeconds) 
    case writeOnly
}
// ExpiredAfterSeconds - read the data from the cache only if they live there for no longer than the specified number of seconds

BestPractice to create such an object, declare an extension on the CacheAssociation type with a constraint on T

Example:

extension CacheAssociation where T == News { 
    static func news(with id: Int, rule: CacheRule) -> CacheAssociation<News> {
        return .init(cacheIdentifier: "\(id)", rule: rule, groupId: nil)
    } 
}

2. Set CacheAssociation<T> on Observable/Single

// somewhere in code 
func loadNews(by id: Int) -> Single<News> { ... }

loadNews(by: id)
  .associate(with: .news(with: id, .readWrite(30))
  .subscribe(onComplete: { ... })
  .disposed(by: bag)

// The handler will first look up the data in the cache by id and if they are there for no longer than 30 seconds, it will return them
// otherwise execute the original `loadNews (by: id)` after it is successful
// data will be written (cacheRule = .readWrite) to the cache.

CacheContainer

For managing caches, use singleton CacheContainer, it allows you to clear all caches, disable caching whole, or turn on logging. The current instance is accessible via CacheContainer.instanceLazyInit

CacheGroupId Cache Groups

Caches can be combined into groups. For a group, you can specify a set of options.

Using options you can, for example, limit the number of caches in a group.

CacheContainer.instanceLazyInit.set(options: [.maxGroupCaches(5)], for: "news") 

// When creating CacheAssociation, you need to specify a group
extension CacheAssociation where T == News {
     static func news (with id: Int, rule: CacheRule) -> CacheAssociation <News> {
         return .init (cacheIdentifier: "\ (id)", rule: rule, groupId: "news") // specify the group
     }
}

func loadNewsData(for id: Int) {
  loadNews(by: id)
    .associate(with: .news(with: id, .readWrite(30))
    .subscribe(onComplete: { ... })
    .disposed(by: bag)
}
 
loadNewsData(for: 101) 
loadNewsData(for: 102) 
loadNewsData(for: 103) 
loadNewsData(for: 104) 
loadNewsData(for: 105) 
loadNewsData(for: 106)
// There will be 5 caches in memory. If we assume that the requests were executed in order after
// run the last cache for 101 retire

rxobservablecache's People

Contributors

alexejn avatar bently93 avatar venvear avatar

Stargazers

 avatar Pavel Lyskov avatar Vasily avatar  avatar  avatar

Watchers

James Cloos avatar  avatar

Forkers

egeniq-forks

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.