block_cache 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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in block_cache.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "block_cache/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "block_cache"
7
+ s.version = BlockCache::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Blake Chambers"]
10
+ s.email = ["chambb1@gmail.com"]
11
+ s.homepage = "http://rubygems.org/gems/block_cache"
12
+ s.summary = %q{uses mongo_mapper to cache block response strings.}
13
+ s.description = %q{uses mongo_mapper to cache block response strings. Use case: wrap your twitter api call with a block, if twitter bugs out, raise an error and the last value will be returned}
14
+
15
+ s.add_dependency('mongo_mapper')
16
+ s.add_development_dependency "rspec"
17
+ # s.rubyforge_project = "block_cache"
18
+
19
+ s.files = `git ls-files`.split("\n")
20
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ s.require_paths = ["lib"]
23
+ end
@@ -0,0 +1,3 @@
1
+ module BlockCache
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,68 @@
1
+ =begin
2
+
3
+ block_cache methods for caching single block responses in mongo
4
+ planned to support mongoid in the future. :D -blake
5
+
6
+ attributes:
7
+ * block_key: basic key passed to the cache_overwrite_by method that
8
+ will store and overwrite by that valid. could easily use
9
+ the request.uri for content cached as json or something as
10
+ such.
11
+ * block_response: basic block of content that is returned by the block call.
12
+
13
+ WARNING: BLOCKS THAT RETURN NIL WILL OVERWRITE THE DATA. TO BE SURE RAISE ERRORS
14
+ WHEN EXITING BLOCKS WITH NIL DATA. THIS LIBRARY CURRENTLY DOSESNT PREVENT
15
+ THIS EXCEPTION.
16
+
17
+ =end
18
+
19
+ module BlockCache
20
+ CACHE_NIL_RESPONSES = true
21
+ HOPTOAD_SUPPORTED=begin
22
+ require 'hoptoad_notifier' ;; true
23
+ rescue
24
+ false
25
+ end
26
+
27
+ def self.included(receiver)
28
+ receiver.send :include, ControllerMethods
29
+ end
30
+
31
+ module ControllerMethods
32
+ class NilResponseError < Exception ;; end
33
+ def cache_overwrite_by(key, &block)
34
+ raise ArgumentError, "needs a key and block" unless key && block
35
+
36
+ return begin
37
+ response_body = block.call()
38
+
39
+ raise NilResponseError, "you returned a nil response" if response_body.nil? and not CACHE_NIL_RESPONSES
40
+
41
+ BlockCache::Record.cache_by_key(key, response_body)
42
+ rescue => exception
43
+ logger.info "block cache error(#{exception.to_s}) returned, rendering from mongo."
44
+ logger.debug "Backtrace:\n\n#{exception.backtrace}\n\n"
45
+ HoptoadNotifier.notify_hoptoad(exception) if RAILS_ENV["production"] and HOPTOAD_SUPPORTED
46
+
47
+ return BlockCache::Record.find_by_block_key(key).try(:block_response)
48
+ end
49
+ end
50
+ end
51
+
52
+ class Record
53
+ include MongoMapper::Document
54
+
55
+ key :block_key, String
56
+ key :block_response, String
57
+
58
+ class << self
59
+ def cache_by_key(block_key, response_body)
60
+ record = find_or_initialize_by_block_key(block_key)
61
+ record.block_response = response_body
62
+ record.save!
63
+
64
+ return response_body
65
+ end
66
+ end
67
+ end
68
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: block_cache
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Blake Chambers
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-05 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: mongo_mapper
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rspec
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ description: "uses mongo_mapper to cache block response strings. Use case: wrap your twitter api call with a block, if twitter bugs out, raise an error and the last value will be returned"
50
+ email:
51
+ - chambb1@gmail.com
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ extra_rdoc_files: []
57
+
58
+ files:
59
+ - .gitignore
60
+ - Gemfile
61
+ - Rakefile
62
+ - block_cache.gemspec
63
+ - lib/block_cache.rb
64
+ - lib/block_cache/version.rb
65
+ has_rdoc: true
66
+ homepage: http://rubygems.org/gems/block_cache
67
+ licenses: []
68
+
69
+ post_install_message:
70
+ rdoc_options: []
71
+
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ hash: 3
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ hash: 3
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ requirements: []
93
+
94
+ rubyforge_project:
95
+ rubygems_version: 1.3.7
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: uses mongo_mapper to cache block response strings.
99
+ test_files: []
100
+