Giter Club home page Giter Club logo

caches's Introduction

caches

caches is a Ruby gem, providing a small collection of caches with good performance and hash-like access patterns. Each is named for its cache expiration strategy - when it will drop a key.

Build Status Code Climate

Usage

caches provides the following classes.

Caches::TTL

TTL (Time To Live) remembers values for as many seconds as you tell it. The default is 3600 seconds (1 hour). Sub-seconds are supported; the tests will fail if the value is too low for your machine.

require 'caches/ttl'

h = Caches::TTL.new(ttl: 0.01)
h[:a] = 'aardvark'
h[:a] #=> 'aardvark'
sleep(0.02)
h[:a] #=> nil

Initialization Options

  • With refresh: true, reading a value will reset its timer; otherwise, only writing will.
  • With max_keys: 5, on insertion, it will evict the oldest item if necessary to keep from exceeding 5 keys.

Methods

keys and values work the same as for a hash. They do not check whether each key is current.

memoize method fetches a key if it exists and isn't expired; otherwise, it calculates the value using the block and saves it.

h = Caches::TTL.new(ttl: 5)

# Runs the block
h.memoize(:a) { |k| calculation_for(k) }

# Returns the previously-calculated value
h.memoize(:a) { |k| calculation_for(k) }

sleep(6)

# Runs the block
h.memoize(:a) { |k| calculation_for(k) }

Caches::LRU

LRU (Least Recently Used) remembers as many keys as you tell it to, dropping the least recently used key on each insert after its limit is reached. (Inserts and reads count as a usage; updates do not.)

require 'caches/lru'
h = Caches::LRU.new(max_keys: 3)
h[:a] = "aardvark"
h[:b] = "boron"
h[:c] = "cattail"
h[:d] = "dingo"
puts h[:a] # => nil

Methods Common to All Caches

  • #fetch works like Hash#fetch
  • #memoize returns the key's value, if any; if not, it gets its return value from the block, and sets the key before it returns
  • #stats tells how many cache hits and misses have occurred
  • #clear resets all data and stats

Installation

Add this line to your application's Gemfile:

gem 'caches'

And then execute:

$ bundle

Or install it yourself as:

$ gem install caches

Contributing

See CONTRIBUTING.md

Thanks

caches's People

Contributors

nathanl avatar ugisozols avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

caches's Issues

Caches Thread Safe?

Are caches thread safe? I have enjoyed using the project but have a case where I want a TTL cache to span thread boundaries but don't know if it's safe.

Thanks!

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.