cachedis 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --format Fuubar
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in cachedis.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cachedis (0.0.1)
5
+ redis
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.1.2)
11
+ fuubar (0.0.3)
12
+ rspec (~> 2.0)
13
+ rspec-instafail (~> 0.1.4)
14
+ ruby-progressbar (~> 0.0.9)
15
+ redis (2.1.1)
16
+ rspec (2.5.0)
17
+ rspec-core (~> 2.5.0)
18
+ rspec-expectations (~> 2.5.0)
19
+ rspec-mocks (~> 2.5.0)
20
+ rspec-core (2.5.1)
21
+ rspec-expectations (2.5.0)
22
+ diff-lcs (~> 1.1.2)
23
+ rspec-instafail (0.1.7)
24
+ rspec-mocks (2.5.0)
25
+ ruby-progressbar (0.0.9)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ cachedis!
32
+ fuubar
33
+ redis
34
+ rspec
data/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # Cachedis
2
+
3
+ Cachedis caches your expensive queries to a Redis instance so the next time you fire it off, it'll load directly from cache instead of being run again. Optionally you can provide a cache expirement time.
4
+
5
+ @cachedis = Cachedis.new
6
+
7
+ # performs query the first time, performs cached calls to Redis within the next 60 * 60 seconds
8
+ @cachedis.cachedis 'name-of-expensive-query', :expire => 60 * 60 do
9
+ Post.all.expensive_operation
10
+ end
11
+
12
+ ## Installation and dependencies
13
+
14
+ Dependencies are: `redis-rb`
15
+
16
+ Install with: `gem install cachedis`
17
+
18
+ ## Wishlist
19
+
20
+ * Tight ActiveRecord integration
21
+ * Make expirement optional
22
+ * Serialize using BERT (fall back to YAML)
23
+ * Sexify the API
24
+ - Make it easier to specialize expirement time (e.g. `:expire => 4.hours`)
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ task :default => :spec
5
+
6
+ require 'rspec/core/rake_task'
7
+ RSpec::Core::RakeTask.new
data/cachedis.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "cachedis/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "cachedis"
7
+ s.version = Cachedis::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Simon Hørup Eskildsen"]
10
+ s.email = ["sirup@sirupsen.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Caches expensive database queries in Redis}
13
+ s.description = %q{Instead of running your expensive queries for every page load, let cachedis store them in Redis and fetch them.}
14
+
15
+ s.rubyforge_project = "cachedis"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ # Dependencies
23
+ s.add_dependency('redis')
24
+
25
+ # Development dependencies
26
+ s.add_development_dependency('rspec')
27
+ s.add_development_dependency('fuubar')
28
+ end
@@ -0,0 +1,3 @@
1
+ class Cachedis
2
+ VERSION = "0.0.1"
3
+ end
data/lib/cachedis.rb ADDED
@@ -0,0 +1,24 @@
1
+ require 'redis'
2
+ require 'yaml'
3
+
4
+ class Cachedis
5
+ attr_reader :redis_instance
6
+
7
+ def initialize(options = {})
8
+ redis(options)
9
+ end
10
+
11
+ def cachedis(name, options = {}, &block)
12
+ result = yield
13
+
14
+ return redis.get name if redis.exists name
15
+
16
+ redis.set name, result.to_yaml
17
+ redis.expire name, options[:expire] || 60 * 60 # set expire to user specified or one minute
18
+ end
19
+
20
+ private
21
+ def redis(options = {})
22
+ @redis_instance ||= Redis.new(options)
23
+ end
24
+ end
data/spec/base_spec.rb ADDED
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cachedis do
4
+ include HelperMethods
5
+
6
+ before do
7
+ @cachedis = Cachedis.new
8
+ end
9
+
10
+ describe 'when setting something' do
11
+ it 'sets without errors' do
12
+ with_no_cache
13
+ @cachedis.redis_instance.should_receive(:set).exactly(1).times.and_return(['element', 'element 2'].to_yaml)
14
+
15
+ lambda {
16
+ @cachedis.cachedis 'expensive-query' do
17
+ ['element', 'element 2']
18
+ end }.should_not raise_error
19
+ end
20
+ end
21
+
22
+ describe 'when setting and later retrieving something' do
23
+ it 'retrieves from redis cache' do
24
+ with_cache('query')
25
+ @cachedis.redis_instance.should_not_receive(:set)
26
+
27
+ result = @cachedis.cachedis 'expensive-query' do
28
+ "query"
29
+ end
30
+
31
+ result.should == "query".to_yaml
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,15 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'cachedis'
4
+ require 'rspec'
5
+
6
+ module HelperMethods
7
+ def with_no_cache
8
+ @cachedis.redis_instance.should_receive(:exists).exactly(1).times.and_return(nil)
9
+ end
10
+
11
+ def with_cache(cache)
12
+ @cachedis.redis_instance.should_receive(:exists).exactly(1).times.and_return(true)
13
+ @cachedis.redis_instance.should_receive(:get).exactly(1).times.and_return(cache.to_yaml)
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cachedis
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - "Simon H\xC3\xB8rup Eskildsen"
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-03-07 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: redis
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: rspec
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :development
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: fuubar
48
+ prerelease: false
49
+ requirement: &id003 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ type: :development
58
+ version_requirements: *id003
59
+ description: Instead of running your expensive queries for every page load, let cachedis store them in Redis and fetch them.
60
+ email:
61
+ - sirup@sirupsen.com
62
+ executables: []
63
+
64
+ extensions: []
65
+
66
+ extra_rdoc_files: []
67
+
68
+ files:
69
+ - .gitignore
70
+ - .rspec
71
+ - Gemfile
72
+ - Gemfile.lock
73
+ - README.md
74
+ - Rakefile
75
+ - cachedis.gemspec
76
+ - lib/cachedis.rb
77
+ - lib/cachedis/version.rb
78
+ - spec/base_spec.rb
79
+ - spec/spec_helper.rb
80
+ has_rdoc: true
81
+ homepage: ""
82
+ licenses: []
83
+
84
+ post_install_message:
85
+ rdoc_options: []
86
+
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ segments:
103
+ - 0
104
+ version: "0"
105
+ requirements: []
106
+
107
+ rubyforge_project: cachedis
108
+ rubygems_version: 1.3.7
109
+ signing_key:
110
+ specification_version: 3
111
+ summary: Caches expensive database queries in Redis
112
+ test_files:
113
+ - spec/base_spec.rb
114
+ - spec/spec_helper.rb