pg_cache_key 0.1.5 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +19 -0
- data/lib/pg_cache_key/version.rb +1 -1
- data/lib/pg_cache_key.rb +8 -22
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 995d11055ccb51441104752165aa4d6e98822cbe
|
4
|
+
data.tar.gz: d4dd6e20765a25e16a04eb124a437ec83fc3bf35
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '02802c91e0378b83668dd11854a425fdcd96244c50e9be3f68ba59140ebc4b21998d9ad2722233709b387d84b7a5b0f4a0d796d88643348ca136ee4c34618bfe'
|
7
|
+
data.tar.gz: 945a514b98459e782d6e34cd6c5a8a90e94735d948c4789dd271edca462c6551e78b7e73c17bfb74bfe6b7a86ed5f0e3a9e19aaa360bae5408fb87ea076677e7
|
data/README.md
CHANGED
@@ -42,6 +42,22 @@ without includes: 10 items in collection ~ x1.2 faster, 20 ~ x1.25, 50 ~ x1.5, 1
|
|
42
42
|
|
43
43
|
with includes: 10 items in collection ~ x3.6 faster, 20 ~ x4, 50 ~ x5.6, 1000 ~ x32
|
44
44
|
|
45
|
+
Benchmark code can look like this:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
def cache_key_vs_db_checksum(n, model_or_relation = Request)
|
49
|
+
Benchmark.ips do |bm|
|
50
|
+
|
51
|
+
bm.report(:direct_cache_key_rnd) { ApplicationController.new.fragment_cache_key( model_or_relation.limit(n).random().old_cache_key ) ; :done }
|
52
|
+
bm.report(:checksummed_cache_key_rnd ) { ApplicationController.new.fragment_cache_key( model_or_relation.limit(n).random().new_cash_key ); :done }
|
53
|
+
|
54
|
+
bm.compare!
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
```
|
59
|
+
Rem: since gem replaces cache_key, this code will need some changes to benchmark on your machine
|
60
|
+
|
45
61
|
Of course this is not the numbers for whole page rendering, but it's noticeable.
|
46
62
|
|
47
63
|
One of my real page goes from 0.8+ sec, for largest available pagination, to 0.6 sec per page
|
@@ -72,9 +88,12 @@ It works out of the box.
|
|
72
88
|
По идее работает само из коробки, но если будут траблы - пишите.
|
73
89
|
|
74
90
|
## Testing
|
91
|
+
rake spec
|
92
|
+
|
75
93
|
Tested only query string for simple case. In your project you may test it on more specific query.
|
76
94
|
|
77
95
|
## Testing(рус)
|
96
|
+
rake spec
|
78
97
|
Тестируется только строка запроса для самого простого случая + добавление метода в активрекорд релейшен.
|
79
98
|
Стабильность на сложных запросах специфических для вашего приложения можете добавить в свои тесты.
|
80
99
|
|
data/lib/pg_cache_key/version.rb
CHANGED
data/lib/pg_cache_key.rb
CHANGED
@@ -1,24 +1,14 @@
|
|
1
1
|
require "pg_cache_key/version"
|
2
2
|
|
3
|
-
# In rails 5 it can be
|
4
|
-
# module ActiveRecord
|
5
|
-
# module CollectionCacheKey
|
6
|
-
# def collection_cache_key(collection = all, timestamp_column = :updated_at) # :nodoc:
|
7
|
-
# # why use connection.execute instead of doing collection.select because of an order. if you using some order on your scope
|
8
|
-
# # then columns you using to order must appear in the GROUP BY clause or be used in an aggregate function or you will get an error
|
9
|
-
# cache_columns = [timestamp_column, :id]
|
10
|
-
# @cache_keys ||= {}
|
11
|
-
# # we need to add select cache_columns explicitly because if relation has includes it might transform columns to aliases
|
12
|
-
# @cache_keys[timestamp_column] ||= connection.execute( "SELECT md5(string_agg( #{cache_columns.map{|fld| "\"t\".\"#{fld}\"::text" }.join('||')}, '') ) as cache_key
|
13
|
-
# FROM (#{ collection.select(cache_columns).try(:to_sql) }) t" )[0]['cache_key']
|
14
|
-
# end
|
15
|
-
# end
|
16
|
-
# end
|
17
|
-
|
18
3
|
module PgCacheKey
|
19
4
|
def cache_key_raw_sql( timestamp_column = :updated_at )
|
20
|
-
|
21
|
-
|
5
|
+
# Rem 1: why use connection.execute instead of doing collection.select because of an order. if you using some order on your scope
|
6
|
+
# then columns you using to order must appear in the GROUP BY clause or be used in an aggregate function or you will get an error
|
7
|
+
# Rem 2: we need to add select( cache_columns ) explicitly because if relation include it might transform columns to aliases
|
8
|
+
# and we must also select them as uniq-aliase so PG wouldn't be confused
|
9
|
+
# 'ckc' means cache key column :)
|
10
|
+
"SELECT md5(string_agg( t.ckc_#{timestamp_column}||t.ckc_id, '') ) as cache_key FROM (#{
|
11
|
+
select( [timestamp_column, :id].map{|ckc| "#{table_name}.#{ckc}::text as ckc_#{ckc}" } ).try(:to_sql) }) t"
|
22
12
|
end
|
23
13
|
end
|
24
14
|
|
@@ -40,12 +30,8 @@ module ActiveRecord
|
|
40
30
|
include PgCacheKey
|
41
31
|
|
42
32
|
def cache_key(timestamp_column = :updated_at)
|
33
|
+
return "#{self.class.to_s.underscore}/blank" if blank?
|
43
34
|
@cache_keys ||= {}
|
44
|
-
# Rem 1: why use connection.execute instead of doing collection.select because of an order. if you using some order on your scope
|
45
|
-
# then columns you using to order must appear in the GROUP BY clause or be used in an aggregate function or you will get an error
|
46
|
-
# Rem 2: we need to add select( cache_columns ) explicitly because if relation has includes it might transform columns to aliases
|
47
|
-
# and we must also select them as uniq-aliase so PG wouldn't be confused
|
48
|
-
# 'ckc' means cache key column :)
|
49
35
|
@cache_keys[timestamp_column] ||= connection.execute( cache_key_raw_sql(timestamp_column) )[0]['cache_key']
|
50
36
|
end
|
51
37
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pg_cache_key
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- alekseyl
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-06-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -133,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
133
133
|
version: '0'
|
134
134
|
requirements: []
|
135
135
|
rubyforge_project:
|
136
|
-
rubygems_version: 2.
|
136
|
+
rubygems_version: 2.6.8
|
137
137
|
signing_key:
|
138
138
|
specification_version: 4
|
139
139
|
summary: collection_cache_key replacement for system which uses PG as DB
|