From 7d75c1ff7243671b0911f7a3969b3b63993f66aa Mon Sep 17 00:00:00 2001 From: DannyB Date: Wed, 25 May 2016 17:00:03 +0000 Subject: [PATCH] initial commit --- .gitignore | 8 +++ .rspec | 2 + .travis.yml | 19 ++++++ Gemfile | 4 ++ LICENSE | 21 +++++++ README.md | 117 +++++++++++++++++++++++++++++++++++ Runfile | 9 +++ lib/lightly.rb | 2 + lib/lightly/lightly.rb | 69 +++++++++++++++++++++ lib/lightly/version.rb | 3 + lightly.gemspec | 23 +++++++ spec/README.md | 7 +++ spec/lightly/lightly_spec.rb | 94 ++++++++++++++++++++++++++++ spec/spec_helper.rb | 6 ++ 14 files changed, 384 insertions(+) create mode 100644 .gitignore create mode 100644 .rspec create mode 100644 .travis.yml create mode 100644 Gemfile create mode 100644 LICENSE create mode 100644 README.md create mode 100644 Runfile create mode 100644 lib/lightly.rb create mode 100644 lib/lightly/lightly.rb create mode 100644 lib/lightly/version.rb create mode 100644 lightly.gemspec create mode 100644 spec/README.md create mode 100644 spec/lightly/lightly_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0156c40 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +/cache +/coverage +/dev +/doc +/gems +/Gemfile.lock +/.yardoc +*.gem \ No newline at end of file diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..624a7f7 --- /dev/null +++ b/.rspec @@ -0,0 +1,2 @@ +--color +--format documentation diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..0618da6 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,19 @@ +language: ruby + +rvm: + - "2.0.0" + - "2.2.1" + +notifications: + email: false + +script: bundle exec rspec + +# notifications: +# email: +# recipients: +# - one@example.com +# - other@example.com +# on_success: [always|never|change] # default: change +# on_failure: [always|never|change] # default: always + diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..f68a903 --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source "https://rubygems.org" + +gemspec + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0b9e852 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) Danny Ben Shitrit + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..1093e96 --- /dev/null +++ b/README.md @@ -0,0 +1,117 @@ +Lightly - Ruby File Cache +================================================== + +[![Gem](https://img.shields.io/gem/v/lightly.svg?style=flat-square)](https://rubygems.org/gems/lightly) +[![Travis](https://img.shields.io/travis/DannyBen/lightly.svg?style=flat-square)](https://travis-ci.org/DannyBen/lightly) +[![Code Climate](https://img.shields.io/codeclimate/github/DannyBen/lightly.svg?style=flat-square)](https://codeclimate.com/github/DannyBen/lightly) +[![Gemnasium](https://img.shields.io/gemnasium/DannyBen/lightly.svg?style=flat-square)](https://gemnasium.com/DannyBen/lightly) + +--- + +Lightly is a file cache for performing heavy tasks, lightly. + +--- + +Install +-------------------------------------------------- + +``` +$ gem install lightly +``` + +Or with bundler: + +```ruby +gem 'lightly' +``` + +Usage +-------------------------------------------------- + +```ruby +require 'lightly' + +lightly = Lightly.new + +content = lightly.with 'key' do + # Heavy operation here +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: + +```ruby +lightly = Lightly.new dir: 'tmp/my_cache', life: 7200, hash: false +``` + +Or later: + +```ruby +lightly = Lightly.new +lightly.dir = 'tmp/my_cache' +lightly.life = 7200 # seconds +lightly.hash = false +``` + +To check if a key is cached, use the `cached?` method: + +```ruby +lightly = Lightly.new +lightly.cached? 'example' +# => false + +content = lightly.with 'example' do + open('http://example.com').read +end +lightly.cached? 'example' +# => true +``` + +You can enable/disable the cache at any time: + +```ruby +lightly = Lightly.new +lightly.disable +lightly.enabled? +# => false + +content = lightly.with 'example' do + open('http://example.com').read +end +lightly.cached? 'example' +# => false + +lightly.enable +content = lightly.with 'example' do + open('http://example.com').read +end +lightly.cached? 'example' +# => true +``` + +The `key` method is an alias to `with`, if you prefer a different wording: + +```ruby +cache = Lightly.new + +content = cache.key 'example' do + # Heavy operation here +end +``` + +--- + +For a similar gem that provides caching specifically for HTTP downloads, +see the [WebCache gem][1] + + +[1]: https://github.com/DannyBen/webcache \ No newline at end of file diff --git a/Runfile b/Runfile new file mode 100644 index 0000000..c25884a --- /dev/null +++ b/Runfile @@ -0,0 +1,9 @@ +require "runfile-tasks" +require_relative 'lib/lightly' + +name "Lightly Developer Toolbelt" +summary "Runfile tasks for building the Lightly gem" +version Lightly::VERSION + +RunfileTasks::RubyGems.all 'lightly' +RunfileTasks::Testing.rspec diff --git a/lib/lightly.rb b/lib/lightly.rb new file mode 100644 index 0000000..51cffac --- /dev/null +++ b/lib/lightly.rb @@ -0,0 +1,2 @@ +require 'lightly/version' +require 'lightly/lightly' diff --git a/lib/lightly/lightly.rb b/lib/lightly/lightly.rb new file mode 100644 index 0000000..68854ab --- /dev/null +++ b/lib/lightly/lightly.rb @@ -0,0 +1,69 @@ +require 'digest/md5' +require 'fileutils' + +class Lightly + attr_accessor :dir, :life, :hash + + def initialize(opts={}) + @dir = opts[:dir] || 'cache' + @life = opts[:life] || 3600 + @hash = opts.key?(:hash) ? opts[:hash] : true + @enabled = true + end + + def with(key, &block) + return load key if cached?(key) && enabled? + + content = block.call + save key, content if enabled? + content + end + alias_method :key, :with + + def flush + return false if dir == '/' || dir.empty? + FileUtils.rm_rf dir + end + + def enabled? + @enabled + end + + def enable + @enabled = true + end + + def disable + @enabled = false + end + + def cached?(key) + path = get_path key + File.exist? path and !expired? path + end + + private + + def save(key, content) + FileUtils.mkdir_p dir + path = get_path key + File.open path, 'wb' do |f| + f.write Marshal.dump content + end + end + + def load(key) + Marshal.load File.binread(get_path key) + end + + def get_path(key) + key = Digest::MD5.hexdigest(key) if hash + File.join dir, key + end + + def expired?(path) + expired = life > 0 && File.exist?(path) && Time.new - File.mtime(path) >= life + FileUtils.rm path if expired + expired + end +end diff --git a/lib/lightly/version.rb b/lib/lightly/version.rb new file mode 100644 index 0000000..5c29c01 --- /dev/null +++ b/lib/lightly/version.rb @@ -0,0 +1,3 @@ +class Lightly + VERSION = "0.1.1" +end \ No newline at end of file diff --git a/lightly.gemspec b/lightly.gemspec new file mode 100644 index 0000000..c9af86e --- /dev/null +++ b/lightly.gemspec @@ -0,0 +1,23 @@ +lib = File.expand_path('../lib', __FILE__) +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) +require 'lightly/version' + +Gem::Specification.new do |s| + s.name = 'lightly' + s.version = Lightly::VERSION + s.date = Date.today.to_s + s.summary = "File cache for performing heavy tasks, lightly." + s.description = "Easy to use file cache" + s.authors = ["Danny Ben Shitrit"] + s.email = 'db@dannyben.com' + s.files = Dir['README.md', 'lib/**/*.*'] + s.homepage = 'https://github.com/DannyBen/lightly' + s.license = 'MIT' + s.required_ruby_version = ">= 2.0.0" + + s.add_development_dependency 'runfile', '~> 0.7' + s.add_development_dependency 'runfile-tasks', '~> 0.4' + s.add_development_dependency 'rspec', '~> 3.4' + s.add_development_dependency 'simplecov', '~> 0.11' + s.add_development_dependency 'byebug', '~> 9.0' +end diff --git a/spec/README.md b/spec/README.md new file mode 100644 index 0000000..67d595e --- /dev/null +++ b/spec/README.md @@ -0,0 +1,7 @@ +Testing +================================================== + +Run tests with: + + $ bundle exec run spec + diff --git a/spec/lightly/lightly_spec.rb b/spec/lightly/lightly_spec.rb new file mode 100644 index 0000000..9456d1a --- /dev/null +++ b/spec/lightly/lightly_spec.rb @@ -0,0 +1,94 @@ +require 'spec_helper' + +describe Lightly do + let(:lightly) { Lightly.new } + + before do + lightly.flush + end + + describe '#new' do + it "sets default properties" do + expect(lightly).to be_enabled + expect(lightly.life).to eq 3600 + expect(lightly.dir).to eq 'cache' + expect(lightly.hash).to be true + end + + it "accepts initialization properties" do + lightly = Lightly.new dir: 'store', life: 120, hash: false + expect(lightly.life).to eq 120 + expect(lightly.dir).to eq 'store' + expect(lightly.hash).to be false + end + end + + describe '#with' do + it "skips caching if disabled" do + lightly.disable + lightly.with('key') { 'content' } + expect(Dir['cache/*']).to be_empty + end + + it "creates a cache folder" do + expect(Dir).not_to exist 'cache' + lightly.with('key') { 'content' } + expect(Dir).to exist 'cache' + end + + it "saves a file" do + lightly.with('key') { 'content' } + expect(Dir['cache/*']).not_to be_empty + end + + it "loads from cache" do + lightly.with('key') { 'content' } + expect(lightly).to be_cached 'key' + expect(lightly).to receive(:load) + lightly.with('key') { 'new, irrelevant content' } + end + + it "returns content from cache" do + lightly.with('key') { 'content' } + expect(lightly).to be_cached 'key' + content = lightly.with('key') { 'new, irrelevant content' } + expect(content).to eq 'content' + end + end + + describe '#key' do + it "behaves like #with" do + expect(lightly.method(:key)).to eq lightly.method(:with) + end + end + + describe '#cached?' do + context 'with a cached key' do + it "returns true" do + lightly.with('key') { 'content' } + expect(lightly).to be_cached 'key' + end + end + + context 'with an uncached key' do + it "returns false" do + expect(lightly).not_to be_cached 'key' + end + end + end + + describe '#enable' do + it "enables cache handling" do + lightly.enable + expect(lightly).to be_enabled + end + end + + describe '#disable' do + it "disables cache handling" do + lightly.disable + expect(lightly).not_to be_enabled + end + end + +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..75d097e --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,6 @@ +require 'simplecov' +SimpleCov.start + +require 'rubygems' +require 'bundler' +Bundler.require :default, :development