Code Monkey home page Code Monkey logo

cache-hash's Introduction

Cache Hash

A simple key value store where pairs can expire after a set interval

Installation

Add this to your application's shard.yml:

dependencies:
  cache_hash:
    github: samueleaton/cache_hash

Usage Example

require "cache_hash"

cache_hash = CacheHash(String).new(2.seconds)

cache_hash.set "city1", "Seattle"
sleep 1 # one second elapsed

cache_hash.set "city2", "Hong Kong"
sleep 1 # two seconds elapsed

cache_hash.get "city1" #=> nil
cache_hash.get "city2" #=> "Hong Kong"

API

CacheHash(V)

Defines the type(s) for the values.

Example

CacheHash(String).new(1.minute)

.new(cache_time_span : Time::Span)

Creates a new instance of CacheHash and sets the cache interval.

.set(key : String, value : V)

Adds a key/value pair to the hash, where V is the type(s) defined at CacheHash(V), and saves the time of the action.

.get(key : String) : V | Nil

Returns the value for the the associated key. If the pair is stale (expired) or does not exists, it returns nil. If it exists but is expired, it is deleted before returning nil.

.purge_stale

Removes all stale key/value pairs from the hash.

.keys() : Array(String)

Runs purge_stale and returns an array of all the the non-stale keys.

.fresh?(key : String) : Bool

Returns true if the key/value pair exists and is not stale. If the pair is stale (expired) or does not exists, it returns false. If it exists but is expired, it is deleted before returning false.

.time(key : String) : Time

Returns the time the key/value pair was cached. If the pair is stale (expired) or does not exists, it returns nil. If it exists but is expired, it is deleted before returning nil.

.refresh(key : String) : V | Nil

Refreshes the time for the key/value pair and returns the hash value if successful, otherwise returns nil.

Contributing

  1. Fork it ( https://github.com/samueleaton/cache_hash/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

cache-hash's People

Contributors

petoem avatar samueleaton avatar

Watchers

 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.