Giter Club home page Giter Club logo

ohmycache's Introduction

OhMyCache

Build Status

OhMyCache is a javascript library that uses LocalStorage and SessionStorage for cache datas on your application without dependencies

Documentation

Official documentation

Requirements

Web browser with LocalStorage and SessionStorage :

  • Chrome 5+
  • Firefox 3.5+
  • IE 8+
  • Safari 4+
  • Opera 10.50+
  • Safari Mobile iOS 3.2
  • Android 2.1+

Installation

With npm

npm install OhMyCache

With bower

bower install OhMyCache

Other method

Download this repository and copy files on your project (not recommended)

Load the lib

ES2015 (babel)

import {Local, Session} from 'OhMyCache'

// quick use
Local.set('key', 'val')
Session.set('key', 'val')

With require function of node

var OhMyCache = require('OhMyCache')

// quick use
OhMyCache.Local.set('key', 'val')
OhMyCache.Session.set('key', 'val')

In html

<html>
  <head>
        <!-- npm -->
        <script src="node_modules/ohmycache/dist/bundle.js"></script>

        <!-- bower -->
        <script src="bower_components//ohmycache/dist/bundle.js"></script>

        <!-- other method -->
    <script src="lib-path/ohmycache/dist/bundle.js"></script>
  </head>
</html>

Quick Start

Store data in local storage

OhMyCache.Local.set('key', 'local')

Store data in session storage

OhMyCache.Session.set('key', 'session')

Get data in local storage

OhMyCache.Local.get('key') // return 'local'

Get data in session storage

OhMyCache.Session.get('key') // return 'session'

Classes

OhMyCache.Local (use localStorage)

The localStorage property allows you to access a local Storage object. localStorage is similar to sessionStorage. The only difference is that, while data stored in localStorage has no expiration time, data stored in sessionStorage gets cleared when the browsing session ends—that is, when the browser is closed.

OhMyCache.Session (use sessionStorage)

The sessionStorage property allows you to access a session Storage object. sessionStorage is similar to Window.localStorage, the only difference is while data stored in localStorage has no expiration set, data stored in sessionStorage gets cleared when the page session ends. A page session lasts for as long as the browser is open and survives over page reloads and restores. Opening a page in a new tab or window will cause a new session to be initiated, which differs from how session cookies work.

Methods

OhMyCache.Local and OhMyCache.Session share the same API, have access to the same functions.

Set

Add the key to the storage, or update that key's value if it already exists set(key, value, options)

Parameters

  • {string} key : name of the key you want to create/updat
  • {mixed} value : value you want to give the key you are creating/updating (string, int, array, object...)

Options

  • {mixed} expire : (int) the item expires in x seconds // default = false
  • {mixed} readonly : (true) prohibit modification // default = false

Return

Boolean : (true) success || (false) fail

Exemples

// String
OhMyCache.Local.set('key', 'value')

// Array
OhMyCache.Local.set('key', [1,2,3])

// Object
OhMyCache.Local.set('key', {fistname: 'James', lastname: 'Bond'})

// With expiration
OhMyCache.Local.set('key', 'value', {expire: 3600}) // data expires in 1 hour

// ReadOnly
OhMyCache.Local.set('key', 'value', {readonly: true}) // return true
OhMyCache.Local.set('key', 'change') // return false
OhMyCache.Local.get('key') // return 'value'

Get

Get the value, remove if expired get(key)

Parameters

  • {string} key

Return

Mixed : the value OR null if the data has expired or not exist

Exemples

OhMyCache.Local.set('key', 'value')

OhMyCache.Local.get('key') // 'value'
OhMyCache.Session.get('key') // null

OhMyCache.Local.set('key', 'value', {expire: 1})
// sleep 2 secondes or more
OhMyCache.Local.get('key') // null

GetAll

Get all items, remove all expired items getAll()

Return

Object : all items {key: value, key2: value2...}

Exemples

OhMyCache.Local.set('k1', 'v1')
OhMyCache.Local.set('k2', 'v2', {readonly: true})
OhMyCache.Local.set('k3', 'v3', {expire: 1})

// sleep 2 secondes or more
OhMyCache.Local.getAll() // {k1: 'v1', k2: 'v2'}

Keys

Return all item's keys, doen't remove expired items keys()

Return

Array : all item's keys

Exemples

OhMyCache.Local.set('k1', 'v1')
OhMyCache.Local.set('k2', 'v2', {readonly: true})
OhMyCache.Local.set('k3', 'v3', {expire: 1})

// sleep 2 secondes or more
OhMyCache.Local.keys() // ['k1', 'k2', 'k3']

Remove

Remove item if don't readonly remove(key)

Parameters

  • {string} key

Return

Boolean : (true) success || (false) fail

Exemples

OhMyCache.Local.set('key', 'value')
OhMyCache.Local.get('key') // 'value'
OhMyCache.Local.remove('key') // true
OhMyCache.Local.get('key') // null

// ReadOnly
OhMyCache.Local.set('key', 'value', {readonly: true})
OhMyCache.Local.get('key') // 'value'
OhMyCache.Local.remove('key') // false
OhMyCache.Local.get('key') // 'value'

Clear

Remove all items clear(onlyExpired)

Parameters

  • {boolean} onlyExpired : if true remove only expired items else remove all items, default false

Return

Boolean : success

Exemples

OhMyCache.Local.set('key', 'value')
OhMyCache.Local.set('key2', 'value', {readonly: true})

OhMyCache.Local.clear() // true

OhMyCache.Local.get('key') // null
OhMyCache.Local.get('key2') // null

isExpired

Indicates whether the value has expired isExpired(key)

Parameters

  • {string} key

Return

Boolean : (true) is expired || (false) is not expired

Exemples

// No expiration
OhMyCache.Local.set('key', 'value')
OhMyCache.Local.isExpired('key') // false

// No expired
OhMyCache.Local.set('key', 'value', {expire: 999999})
OhMyCache.Local.isExpired('key') // false

// Expired
OhMyCache.Local.set('key', 'value', {expire: 1})
// sleep 1 second or more
OhMyCache.Local.isExpired('key') // true

isReadonly

Indicates whether the value is readonly isReadonly(key)

Parameters

  • {string} key

Return

Boolean : (true) is read only || (false) is not read only

Exemples

// No readonly
OhMyCache.Local.set('key', 'value')
OhMyCache.Local.isReadonly('key') // false

// is readonly
OhMyCache.Local.set('key', 'value', {readonly: true})
OhMyCache.Local.isReadonly('key') // true

ohmycache's People

Contributors

ozee31 avatar

Watchers

James Cloos avatar Niklas 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.