table_checksum 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Filip Zachar
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,18 @@
1
+ table_checksum
2
+ ==============
3
+
4
+ Gem adds methods to the ActiveRecord::Base classes to easily generate checksum of some table using CREATE CHECKSUM \`table_name\`; syntax
5
+
6
+ User.checksum # => ["database.users", 2513815180]
7
+
8
+ This is useful for fragment caching when you list all records from table in cached partial
9
+
10
+ <%= cache [@event, User.checksum] do %>
11
+ ...
12
+ <%= f.collection_select :user_id, @users || User.all, :id, :name %>
13
+ ...
14
+ <% end %>
15
+
16
+ It also uses PerRequestCache for performance.
17
+
18
+ I tried to write the PerRequestCache as much threadsafe as i could
@@ -0,0 +1,34 @@
1
+ module TableChecksum
2
+ class PerRequestCache
3
+ class << self
4
+ def open_the_cache
5
+ @cache ||= {}
6
+ @cache[self.cache_key] ||= {}
7
+ end
8
+
9
+ def cache_key
10
+ "#{Process.pid}_#{Thread.current.object_id}".to_sym
11
+ end
12
+
13
+ def clear_the_cache
14
+ @cache.delete(self.cache_key)
15
+ end
16
+
17
+ def fetch(key, &block)
18
+ return yield if @cache.nil?
19
+ @cache[self.cache_key][key] ||= yield
20
+ end
21
+ end
22
+
23
+ def initialize(app)
24
+ @app = app
25
+ end
26
+
27
+ def call(env)
28
+ self.class.open_the_cache
29
+ @app.call(env)
30
+ ensure
31
+ self.class.clear_the_cache
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,7 @@
1
+ module TableChecksum
2
+ class Railtie < Rails::Railtie
3
+ initializer "table_checksum.insert_middleware" do |app|
4
+ app.config.middleware.use "TableChecksum::PerRequestCache"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,21 @@
1
+ require 'table_checksum/per_request_cache'
2
+ require 'table_checksum/railitie' if defined? Rails
3
+
4
+ module TableChecksum
5
+ def self.for table
6
+ table = table.table_name if table.respond_to? :table_name
7
+ PerRequestCache.fetch(table) do
8
+ ActiveRecord::Base.connection.execute("CHECKSUM TABLE #{table}").first
9
+ end
10
+ end
11
+
12
+ module ActiveRecordClassMethods
13
+ def checksum
14
+ TableChecksum.for(self)
15
+ end
16
+ end
17
+ end
18
+
19
+ ActiveRecord::Base.class_eval do
20
+ extend TableChecksum::ActiveRecordClassMethods
21
+ end
@@ -0,0 +1,13 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ Gem::Specification.new do |s|
3
+ s.name = 'table_checksum'
4
+ s.version = '0.0.1'
5
+ s.summary = "Table checksum"
6
+ s.description = "Creates checksum of tables/models using CREATE CHECKSUM sql query"
7
+ s.authors = ["Filip Zachar"]
8
+ s.email = 'tulak45@gmail.com'
9
+ s.homepage = 'http://github.com/tulak/table_checksum'
10
+
11
+ s.require_paths = ["lib"]
12
+ s.files = `git ls-files`.split("\n")
13
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: table_checksum
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Filip Zachar
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-20 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Creates checksum of tables/models using CREATE CHECKSUM sql query
15
+ email: tulak45@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE
21
+ - README.md
22
+ - lib/table_checksum.rb
23
+ - lib/table_checksum/per_request_cache.rb
24
+ - lib/table_checksum/railitie.rb
25
+ - table_checksum.gemspec
26
+ homepage: http://github.com/tulak/table_checksum
27
+ licenses: []
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 1.8.24
47
+ signing_key:
48
+ specification_version: 3
49
+ summary: Table checksum
50
+ test_files: []