Skip to content

janzhou/scalajs-cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cache API for Scala.js backend by Window.localStorage

Install

Add library and resolver in build.sbt

libraryDependencies ++= Seq(
  "org.janzhou" %%% "scalajs-cache" % "0.0.2"
)
resolvers += "Jian Zhou" at "https://raw.githubusercontent.com/janzhou/mvn-repo/release"

Usage

Create Cache

import org.janzhou.scalajs.Cache

object cached extends Cache("cached", 1000, 60 * 60 * 1000) {
  lazy val sub = Cache("cached.sub", 1000, 60 * 60 * 1000)

  def cleanAll = {
    rm()
    sub.rm()
  }
}

Basic Usage

Store data into a "key". The data will be serialized by using uPickle.

cached.set("key", data)

The expiration data can be specified per-key.

cached.set("key", data, expiration)

Advanced Usage

The Future object can be cached.

val newFuture = cached("future", Future{ real job })

This function will return a new Future with the cached value. Otherwise, the real job will be scheduled. This can be used in conjunction with a remote call.

Cache a lambda expression.

val newValue = cached("lambda", { real job })

This function will return the cached value. Otherwise, the real job will be executed.