All Projects → DannyBen → lightly

DannyBen / lightly

Licence: MIT license
Ruby file cache for performing heavy tasks, lightly.

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to lightly

Wavefile
A Ruby gem for reading and writing sound files in Wave format (*.wav)
Stars: ✭ 193 (+421.62%)
Mutual labels:  gem
Unimidi
Realtime MIDI IO for Ruby
Stars: ✭ 229 (+518.92%)
Mutual labels:  gem
memo wise
The wise choice for Ruby memoization
Stars: ✭ 486 (+1213.51%)
Mutual labels:  gem
Squid
A Ruby library to plot charts in PDF files
Stars: ✭ 205 (+454.05%)
Mutual labels:  gem
Localer
Automatic detecting missing I18n translations tool.
Stars: ✭ 219 (+491.89%)
Mutual labels:  gem
Api Fuzzer
API Fuzzer which allows to fuzz request attributes using common pentesting techniques and lists vulnerabilities
Stars: ✭ 238 (+543.24%)
Mutual labels:  gem
Ougai
A Ruby structured logging is capable of handling a message, custom data or an exception easily and generates JSON or human readable logs.
Stars: ✭ 187 (+405.41%)
Mutual labels:  gem
ibandit
Convert national banking details into IBANs, and vice-versa.
Stars: ✭ 90 (+143.24%)
Mutual labels:  gem
Localeapp
Send and retrieve your ruby i18n localizations to the Locale translation service
Stars: ✭ 225 (+508.11%)
Mutual labels:  gem
httpd-plus
Add-ons for the OpenBSD web server
Stars: ✭ 32 (-13.51%)
Mutual labels:  cache-control
Gemsurance
Gem vulnerability checker using rubysec/ruby-advisory-db
Stars: ✭ 207 (+459.46%)
Mutual labels:  gem
Bootsnap
Boot large Ruby/Rails apps faster
Stars: ✭ 2,480 (+6602.7%)
Mutual labels:  gem
Forest Rails
🌱 Rails Liana for Forest Admin
Stars: ✭ 247 (+567.57%)
Mutual labels:  gem
Arctic admin
Responsive Theme for ActiveAdmin
Stars: ✭ 201 (+443.24%)
Mutual labels:  gem
graphql authorize
Authorization helpers for ruby-graphql fields
Stars: ✭ 23 (-37.84%)
Mutual labels:  gem
Flexirest
Flexirest - The really flexible REST API client for Ruby
Stars: ✭ 188 (+408.11%)
Mutual labels:  gem
Victor
Ruby SVG Image Builder
Stars: ✭ 237 (+540.54%)
Mutual labels:  gem
schked
Framework agnostic scheduler to run recurring jobs.
Stars: ✭ 78 (+110.81%)
Mutual labels:  gem
omniauth-multi-provider-saml
An extension to omniauth-saml for handling multiple identity providers
Stars: ✭ 14 (-62.16%)
Mutual labels:  gem
open dota api
Ruby client for Dota 2 from OpenDotaAPI
Stars: ✭ 19 (-48.65%)
Mutual labels:  gem

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
lightly = Lightly.new life: '3h'
response = lightly.get 'key' do
  # Heavy operation here
end

# Static
Lightly.life = '3h'
response = 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 document 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:

lightly.life = 10     # 10 seconds
lightly.life = '20s'  # 20 seconds
lightly.life = '10m'  # 10 minutes
lightly.life = '10h'  # 10 hours
lightly.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 = lightly.get 'test' do
  false
end

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

Related Projects

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

Contributing / Support

If you experience any issue, have a question or a suggestion, or if you wish to contribute, feel free to open an issue.


Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].