-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Complete API re-write.
- Loading branch information
Showing
21 changed files
with
342 additions
and
254 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,20 @@ | ||
source 'https://rubygems.org' | ||
source "https://rubygems.org" | ||
|
||
platforms :ruby_19, :ruby_20, :ruby_21 do | ||
group :development do | ||
gem "celluloid-io" | ||
gem "guard-rspec" | ||
end | ||
|
||
gem "rubocop" | ||
end | ||
|
||
gem "rake" | ||
gem "rspec" | ||
gem "guard-rspec" | ||
gem "rubocop" | ||
gem "rspec", "~> 3.0" | ||
gem "timecop" | ||
|
||
gem "coveralls", :require => false | ||
gem "simplecov", :require => false | ||
|
||
platforms :rbx do | ||
gem "racc" | ||
gem "rubinius-coverage", "~> 2.0" | ||
gem "rubysl", "~> 2.0" | ||
end | ||
|
||
# Specify your gem's dependencies in cachew.gemspec | ||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
require "bundler/gem_tasks" | ||
|
||
require 'rspec/core/rake_task' | ||
require "rspec/core/rake_task" | ||
RSpec::Core::RakeTask.new | ||
|
||
require 'rubocop/rake_task' | ||
Rubocop::RakeTask.new | ||
require "rubocop/rake_task" | ||
RuboCop::RakeTask.new | ||
|
||
task :default => [:spec, :rubocop] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,4 @@ | ||
require "cachew/adapter" | ||
require "cachew/hash" | ||
require "cachew/null" | ||
require "cachew/version" | ||
require "cachew/adapters" | ||
|
||
# Unified cache interface | ||
class Cachew | ||
extend Forwardable | ||
|
||
attr_reader :adapter | ||
|
||
def initialize(store = nil) | ||
@adapter = Adapters.build_adapter_for store | ||
end | ||
|
||
def fetch(key) | ||
has?(key) ? get(key) : set(key, yield) | ||
end | ||
|
||
def_delegators :adapter, :set, :get, :has? | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
require "digest/md5" | ||
require "json" | ||
|
||
module Cachew | ||
# Base class for Cachew adapters. | ||
# | ||
# @example Building custom adapters | ||
# | ||
# class Cachew::Redis < Cache::Adapter | ||
# def initialize(client) | ||
# @client = client | ||
# end | ||
# | ||
# private | ||
# | ||
# def __set__(key, val, ttl) | ||
# val = Marshal.dump val | ||
# | ||
# if 0 == ttl | ||
# @client.set key, val | ||
# else | ||
# @client.setex key, val, ttl | ||
# end | ||
# end | ||
# | ||
# def __get__(key) | ||
# val = @client.get(key) | ||
# val ? Marshal.load(val) : UNDEFINED | ||
# end | ||
# | ||
# def __key__(*) | ||
# "cachew:#{super}" | ||
# end | ||
# end | ||
# | ||
class Adapter | ||
# Internal constant used by `__get__` to notify that value is not in the | ||
# cache or became stale | ||
UNDEFINED = Object.new.freeze | ||
|
||
# @example Usage | ||
# | ||
# cachew.fetch "some_key", :ttl => 60 do | ||
# HTTP.get("http://example.com").to_s | ||
# end | ||
# | ||
def fetch(key, opts = {}) | ||
key = __key__ key, opts | ||
val = __get__ key | ||
|
||
if UNDEFINED.equal? val | ||
val = yield | ||
ttl = opts.fetch(:ttl) { 0 }.to_i | ||
|
||
__set__ key, val, ttl | ||
end | ||
|
||
val | ||
end | ||
|
||
private | ||
|
||
# :nodoc: | ||
def __set__(_key, _val, _ttl) | ||
fail "Not implemented" | ||
end | ||
|
||
# :nodoc: | ||
def __get__(_key) | ||
fail "Not implemented" | ||
end | ||
|
||
# :nodoc: | ||
def __key__(*args) | ||
Digest::MD5.hexdigest JSON.dump args | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require "cachew/adapter" | ||
|
||
module Cachew | ||
# In-memory cache adapter. | ||
class Hash < Adapter | ||
# @param [#to_hash] store Underlying Hash used for storage | ||
def initialize(store = {}) | ||
@store = store.to_hash | ||
end | ||
|
||
private | ||
|
||
# :nodoc: | ||
def __set__(key, val, ttl) | ||
ttl = 0 >= ttl ? 0 : Time.now.to_i + ttl | ||
@store[key] = [val, ttl] | ||
end | ||
|
||
# :nodoc: | ||
def __get__(key) | ||
val, ttl = @store[key] | ||
ttl && (0 == ttl || ttl > Time.now.to_i) ? val : UNDEFINED | ||
end | ||
end | ||
end |
Oops, something went wrong.