Skip to content
Ruby file cache for performing heavy tasks, lightly.
Branch: master
Clone or download
Latest commit 9794cfa Apr 30, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib version bump Dec 23, 2018
spec
.gitignore initial commit May 25, 2016
.rspec initial commit May 25, 2016
.travis.yml maintenance update Apr 30, 2019
Gemfile add prune method Dec 20, 2018
LICENSE initial commit May 25, 2016
README.md maintenance update Apr 30, 2019
Runfile update dependencies Apr 1, 2017
lightly.gemspec add prune method Dec 20, 2018

README.md

Lightly - Ruby File Cache

Gem Version Build Status Maintainability


Lightly is a file cache for performing heavy tasks, lightly.


Install

$ gem install lightly

Or with bundler:

gem 'lightly'

Usage

Lightly can be used both as an instance, and as a static class.

require 'lightly'

# Instance
cache = Lightly.new life: '3h'
response = cache.get 'key' do
  # Heavy operation here
end

# Static
Lightly.life = '3h'
Lightly.get 'key' do
  # Heavy operation here
end

The design intention is to provide both a globally available singleton Lightly object, as well as multiple caching instances, with different settings - depending on the use case.

Note that the examples in this README are all using the instance syntax, but all methods are also available statically.

This is the basic usage pattern:

require 'lightly'

lightly = Lightly.new

content = lightly.get 'key' do
  # Heavy operation here
  entire_internet.download
end

This will look for a cached object with the given key and return it if it exists and not older than 1 hour. Otherwise, it will perform the operation inside the block, and save it to the cache object.

By default, the cached objects are stored in the ./cache directory, and expire after 60 minutes. The cache directory will be created as needed.

In addition, the provided key is hashed to its MD5 representation.

You can change these settings on initialization:

lightly = Lightly.new dir: 'tmp/my_cache', life: 7200, hash: false

Or later:

lightly = Lightly.new
lightly.dir = 'tmp/my_cache'
lightly.life = '1d'
lightly.hash = false

The life property accepts any of these formats:

cache.life = 10     # 10 seconds
cache.life = '20s'  # 20 seconds
cache.life = '10m'  # 10 minutes
cache.life = '10h'  # 10 hours
cache.life = '10d'  # 10 days

To check if a key is cached, use the cached? method:

lightly = Lightly.new
lightly.cached? 'example'
# => false

content = lightly.get 'example' do
  open('http://example.com').read
end

lightly.cached? 'example'
# => true

You can enable/disable the cache at any time:

lightly = Lightly.new
lightly.disable
lightly.enabled? 
# => false

content = lightly.get 'example' do
  open('http://example.com').read
end

lightly.cached? 'example'
# => false

lightly.enable

content = lightly.get 'example' do
  open('http://example.com').read
end

lightly.cached? 'example'
# => true

To flush the cache, call:

lightly = Lightly.new
lightly.flush

To clear the cache for a given key, call:

lightly = Lightly.new
lightly.clear 'example'

To clear all expired keys, call:

lightly = Lightly.new
lightly.prune

If your block returns false or nil, the data will not be cached:

result = cache.get 'test' do
  false
end

puts cache.cached? 'test'
# => false

For a similar gem that provides caching specifically for HTTP downloads, see the WebCache gem

You can’t perform that action at this time.