castache 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
1
+ Gemfile.lock
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ coverage
7
+ InstalledFiles
8
+ lib/bundler/man
9
+ pkg
10
+ rdoc
11
+ spec/reports
12
+ test/tmp
13
+ test/version_tmp
14
+ tmp
15
+
16
+ # YARD artifacts
17
+ .yardoc
18
+ _yardoc
19
+ doc/
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.9.2
5
+ - jruby-19mode
6
+ - rbx-19mode
7
+ services:
8
+ - redis-server
@@ -0,0 +1,9 @@
1
+ ## 0.0.1 (Nov 30, 2012)
2
+
3
+ _initial release!_
4
+
5
+ Features:
6
+ - marshals object to redis with `Catache.set`
7
+ - returns re-hydrated ruby object from redis with `Castache.get`
8
+ - returns re-hydrated ruby object with `Castache.fetch` and will set contents of
9
+ the block you pass in if the object does not exist in the cache
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 StyleSeek Engineering Team
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,44 @@
1
+ # Castache
2
+
3
+ Simple structured cache for ruby apps. Marshals objects to and from Redis
4
+ quickly and painlessly.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'castache'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install castache
19
+
20
+ ## Usage
21
+
22
+ ````ruby
23
+ require 'castache'
24
+ Castache.redis = Redis.new # you can also pass it a redis:// URI string
25
+ # or hash of options to pass to Redis.new
26
+ @object = Castache.fetch 'cache:key' do
27
+ {hello: 'world'} # some expensive operation goes here...
28
+ end
29
+ p @object
30
+ ````
31
+
32
+ ## Project Status
33
+
34
+ - Build: [![Build Status](https://secure.travis-ci.org/styleseek/castache.png?branch=master)](https://travis-ci.org/styleseek/castache)
35
+ - Code Quality: [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/styleseek/castache)
36
+ - Dependencies: [![Dependency Status](https://gemnasium.com/styleseek/castache.png)](https://gemnasium.com/styleseek/castache)
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create new Pull Request
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ desc "run the specs"
4
+ task :spec do
5
+ puts `bundle exec turn -Ispec spec/*_spec.rb`
6
+ end
7
+ task :default => :spec
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'castache/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "castache"
8
+ gem.version = Castache::VERSION
9
+ gem.authors = ["StyleSeek Engineering"]
10
+ gem.email = ["engineering@styleseek.com"]
11
+ gem.description = %q{Simple structured object cache for ruby apps. }
12
+ gem.summary = %q{Simple structured object cache for ruby apps. Marshals objects to and from Redis quickly and painlessly. }
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(spec)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency('redis', '~> 3.0')
21
+
22
+ gem.add_development_dependency('rake', '~> 10')
23
+ gem.add_development_dependency('minitest', '~> 4.2')
24
+ gem.add_development_dependency('ansi', '~> 1.4')
25
+ gem.add_development_dependency('turn', '~> 0.9')
26
+ gem.add_development_dependency('pry', '~> 0.9')
27
+ gem.add_development_dependency('mocha', '~> 0.13')
28
+ gem.add_development_dependency('gem-release', '~> 0.4')
29
+ end
@@ -0,0 +1,52 @@
1
+ require 'redis'
2
+ require 'castache/version'
3
+
4
+ class Castache
5
+ class << self
6
+ attr_accessor :redis
7
+
8
+ def fetch (key, &block)
9
+ unless object = get(key)
10
+ object = set(key, block.call) if block_given?
11
+ end
12
+ object
13
+ end
14
+
15
+ def set(key, object)
16
+ object if @redis.set(key, Marshal.dump(object))
17
+ end
18
+
19
+ def get(key)
20
+ if value = @redis.get(key)
21
+ Marshal.load(value)
22
+ end
23
+ end
24
+
25
+ # Pass it a connected Redis object, a redis:// uri string, or a hash of
26
+ # options for Redis.new
27
+ def redis=(connection)
28
+ if connection.is_a? Redis
29
+ Redis.current = connection
30
+ elsif connection.is_a? String
31
+ Redis.current = connect(connection) if URI(connection).scheme == 'redis'
32
+ elsif connection.is_a? Hash
33
+ Redis.current = Redis.new connection
34
+ end
35
+ @redis = Redis.current
36
+ end
37
+
38
+ def redis
39
+ @redis ||= Redis.current
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ def self.connect(redis_uri)
46
+ uri = URI(redis_uri)
47
+ Redis.new(:host => uri.host,
48
+ :port => uri.port,
49
+ :password => uri.password,
50
+ :thread_safe => true)
51
+ end
52
+ end
@@ -0,0 +1,3 @@
1
+ class Castache
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,79 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Castache do
6
+
7
+ let(:obj) { OpenStruct.new :string => 'Test',
8
+ :integer => 42,
9
+ :float => 1.0,
10
+ :array => [:a,:b,:c],
11
+ :hash => {'key'=>'value'},
12
+ :date => DateTime.strptime('253555860','%s') }
13
+ before do
14
+ Castache.redis = Redis.current
15
+ end
16
+
17
+ after do
18
+ Castache.redis.flushall
19
+ end
20
+
21
+ describe "#redis" do
22
+ it "returns the current redis connection" do
23
+ Castache.redis.must_be_kind_of Redis
24
+ end
25
+ end
26
+
27
+ describe "#redis=" do
28
+ describe "can connect to redis" do
29
+ it "when given a Redis object" do
30
+ (Castache.redis = Redis.new)
31
+ Castache.redis.must_be_kind_of Redis
32
+ end
33
+
34
+ it "when given a uri string" do
35
+ Castache.redis = "redis:://localhost:6379"
36
+ Castache.redis.must_be_kind_of Redis
37
+ end
38
+
39
+ it "when given a hash" do
40
+ Castache.redis = { host: 'localhost', port: 6379, thread_safe: true }
41
+ Castache.redis.must_be_kind_of Redis
42
+ end
43
+ end
44
+ end
45
+
46
+ describe "#write" do
47
+ it "can write an object to the cache" do
48
+ Castache.set('write-test',obj).must_equal obj
49
+ end
50
+ end
51
+
52
+ describe "#read" do
53
+ it "can read an object from the cache" do
54
+ Castache.set('read-test',obj)
55
+ Castache.get('read-test').must_equal obj
56
+ end
57
+
58
+ it "returns nil if the key fails to retrieve an object" do
59
+ Castache.get('read-nil-test').must_be_nil
60
+ end
61
+ end
62
+
63
+ describe "#fetch" do
64
+ before do
65
+ @hash = {:test => 'data'}
66
+ Castache.set('fetch-test', @hash)
67
+ end
68
+
69
+ it "returns an object in the cache, if it already exits" do
70
+ result = Castache.fetch('fetch-test') { obj }
71
+ result.must_equal @hash
72
+ end
73
+
74
+ it "stores the block contents in the cache, if it does not exist" do
75
+ Castache.fetch('fetch-nil-test') { obj }.must_equal obj
76
+ end
77
+ end
78
+
79
+ end
@@ -0,0 +1,8 @@
1
+ require 'castache'
2
+ require 'ostruct'
3
+ begin; require 'turn/autorun'; rescue LoadError; end
4
+ Turn.config do |c|
5
+ c.natural = true
6
+ c.ansi = true
7
+ c.format = :pretty
8
+ end
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: castache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - StyleSeek Engineering
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-30 00:00:00.000000000 -06:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: redis
17
+ requirement: &70340648244680 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: '3.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *70340648244680
26
+ - !ruby/object:Gem::Dependency
27
+ name: rake
28
+ requirement: &70340652778580 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *70340652778580
37
+ - !ruby/object:Gem::Dependency
38
+ name: minitest
39
+ requirement: &70340651333460 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: '4.2'
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *70340651333460
48
+ - !ruby/object:Gem::Dependency
49
+ name: ansi
50
+ requirement: &70340651316260 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: '1.4'
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *70340651316260
59
+ - !ruby/object:Gem::Dependency
60
+ name: turn
61
+ requirement: &70340651552720 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ~>
65
+ - !ruby/object:Gem::Version
66
+ version: '0.9'
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *70340651552720
70
+ - !ruby/object:Gem::Dependency
71
+ name: pry
72
+ requirement: &70340646922760 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '0.9'
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: *70340646922760
81
+ - !ruby/object:Gem::Dependency
82
+ name: mocha
83
+ requirement: &70340635446320 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ~>
87
+ - !ruby/object:Gem::Version
88
+ version: '0.13'
89
+ type: :development
90
+ prerelease: false
91
+ version_requirements: *70340635446320
92
+ - !ruby/object:Gem::Dependency
93
+ name: gem-release
94
+ requirement: &70340640688120 !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ~>
98
+ - !ruby/object:Gem::Version
99
+ version: '0.4'
100
+ type: :development
101
+ prerelease: false
102
+ version_requirements: *70340640688120
103
+ description: ! 'Simple structured object cache for ruby apps. '
104
+ email:
105
+ - engineering@styleseek.com
106
+ executables: []
107
+ extensions: []
108
+ extra_rdoc_files: []
109
+ files:
110
+ - .gitignore
111
+ - .travis.yml
112
+ - CHANGELOG.md
113
+ - Gemfile
114
+ - LICENSE.txt
115
+ - README.md
116
+ - Rakefile
117
+ - castache.gemspec
118
+ - lib/castache.rb
119
+ - lib/castache/version.rb
120
+ - spec/castache_spec.rb
121
+ - spec/spec_helper.rb
122
+ has_rdoc: true
123
+ homepage: ''
124
+ licenses: []
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ! '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ segments:
136
+ - 0
137
+ hash: -3694376980599044599
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ none: false
140
+ requirements:
141
+ - - ! '>='
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ segments:
145
+ - 0
146
+ hash: -3694376980599044599
147
+ requirements: []
148
+ rubyforge_project:
149
+ rubygems_version: 1.3.9.5
150
+ signing_key:
151
+ specification_version: 3
152
+ summary: Simple structured object cache for ruby apps. Marshals objects to and from
153
+ Redis quickly and painlessly.
154
+ test_files:
155
+ - spec/castache_spec.rb
156
+ - spec/spec_helper.rb