thermos 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/thermos/beverage.rb +25 -23
- data/lib/thermos/beverage_storage.rb +20 -0
- data/lib/thermos/dependency.rb +10 -8
- data/lib/thermos/notifier.rb +10 -9
- data/lib/thermos/rebuild_cache_job.rb +8 -0
- data/lib/thermos/refill_job.rb +26 -4
- data/lib/thermos/version.rb +1 -1
- data/lib/thermos.rb +6 -30
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/development.log +23 -19
- data/test/dummy/log/test.log +6233 -5888
- data/test/thermos_test.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3e56270eb773c627e1ecfec8ed6e1ba088a0811b
|
4
|
+
data.tar.gz: 96e2dbe3ea7c9d38b6e8367bc90e7342a38de677
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c32012da14be8ce3f8f481f6a4c534b9a18fa6f6e1af77592b913cd70df8831158d79dd7f8dd5bbb247de98d8cec99d51f5552cb1e31fec0701d9eef33965339
|
7
|
+
data.tar.gz: b4e7ed1511a64989d63b9c80f2b7a14f4cdbc43aac6e593d27ffc69e6516ef1be81d85519ab4d6310f34bf547ef70d3814f99fb41b550ee31e9d1c7bcaf7af01
|
data/lib/thermos/beverage.rb
CHANGED
@@ -1,32 +1,34 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module Thermos
|
2
|
+
class Beverage
|
3
|
+
attr_reader :key, :model, :deps, :action, :lookup_key
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
5
|
+
def initialize(key:, model:, deps:, action:, lookup_key: nil)
|
6
|
+
@key = key
|
7
|
+
@model = model
|
8
|
+
@lookup_key = lookup_key || :id
|
9
|
+
@deps = deps.map do |dep|
|
10
|
+
Dependency.new(model: model, association: dep)
|
11
|
+
end
|
12
|
+
@action = action
|
12
13
|
|
13
|
-
|
14
|
-
|
14
|
+
set_observers
|
15
|
+
end
|
15
16
|
|
16
|
-
|
17
|
-
|
18
|
-
|
17
|
+
def deps_for_class(klass)
|
18
|
+
@deps.select do |dep|
|
19
|
+
dep.klass == klass.name
|
20
|
+
end
|
19
21
|
end
|
20
|
-
end
|
21
22
|
|
22
|
-
|
23
|
+
private
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
def set_observers
|
26
|
+
observe(@model)
|
27
|
+
@deps.each { |dep| observe(dep.klass) }
|
28
|
+
end
|
28
29
|
|
29
|
-
|
30
|
-
|
30
|
+
def observe(model)
|
31
|
+
model.include(Notifier) unless model.included_modules.include?(Notifier)
|
32
|
+
end
|
31
33
|
end
|
32
34
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class Thermos::BeverageStorage
|
2
|
+
include Singleton
|
3
|
+
|
4
|
+
def add_beverage(beverage)
|
5
|
+
@beverages ||= {}
|
6
|
+
@beverages[beverage.key] = beverage
|
7
|
+
end
|
8
|
+
|
9
|
+
def get_beverage(key)
|
10
|
+
@beverages[key]
|
11
|
+
end
|
12
|
+
|
13
|
+
def empty!
|
14
|
+
@beverages = {}
|
15
|
+
end
|
16
|
+
|
17
|
+
def beverages
|
18
|
+
@beverages.values
|
19
|
+
end
|
20
|
+
end
|
data/lib/thermos/dependency.rb
CHANGED
@@ -1,11 +1,13 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module Thermos
|
2
|
+
class Dependency
|
3
|
+
attr_reader :model, :association, :klass, :table
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
def initialize(model:, association:)
|
6
|
+
@model = model
|
7
|
+
@association = association
|
8
|
+
reflection = @model.reflections[@association.to_s]
|
9
|
+
@table = reflection.table_name
|
10
|
+
@klass = reflection.class_name.constantize
|
11
|
+
end
|
10
12
|
end
|
11
13
|
end
|
data/lib/thermos/notifier.rb
CHANGED
@@ -1,14 +1,15 @@
|
|
1
|
-
module Thermos
|
2
|
-
|
1
|
+
module Thermos
|
2
|
+
module Notifier
|
3
|
+
extend ActiveSupport::Concern
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
5
|
+
included do
|
6
|
+
after_save :notify_thermos
|
7
|
+
end
|
7
8
|
|
8
|
-
|
9
|
+
private
|
9
10
|
|
10
|
-
|
11
|
-
|
11
|
+
def notify_thermos
|
12
|
+
RefillJob.perform_later self
|
13
|
+
end
|
12
14
|
end
|
13
|
-
|
14
15
|
end
|
data/lib/thermos/refill_job.rb
CHANGED
@@ -1,6 +1,28 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
module Thermos
|
2
|
+
class RefillJob < ActiveJob::Base
|
3
|
+
def perform(model)
|
4
|
+
refill_primary_caches(model)
|
5
|
+
refill_dependency_caches(model)
|
6
|
+
end
|
7
|
+
|
8
|
+
def refill_primary_caches(model)
|
9
|
+
BeverageStorage.instance.beverages.each do |beverage|
|
10
|
+
if beverage.model == model.class
|
11
|
+
Thermos::RebuildCacheJob.perform_later(beverage.key, model.send(beverage.lookup_key))
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def refill_dependency_caches(model)
|
17
|
+
BeverageStorage.instance.beverages.each do |beverage|
|
18
|
+
deps = beverage.deps.select { |dependency| dependency.klass == model.class }
|
19
|
+
deps.each do |dependency|
|
20
|
+
beverage_models = beverage.model.joins(dependency.association).where(dependency.table => { id: model.id })
|
21
|
+
beverage_models.find_each do |beverage_model|
|
22
|
+
Thermos::RebuildCacheJob.perform_later(beverage.key, beverage_model.send(beverage.lookup_key))
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
5
27
|
end
|
6
28
|
end
|
data/lib/thermos/version.rb
CHANGED
data/lib/thermos.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
require "thermos/beverage"
|
2
|
+
require "thermos/beverage_storage"
|
2
3
|
require "thermos/dependency"
|
3
4
|
require "thermos/notifier"
|
4
5
|
require "thermos/refill_job"
|
6
|
+
require "thermos/rebuild_cache_job"
|
5
7
|
|
6
8
|
module Thermos
|
7
9
|
|
@@ -11,40 +13,14 @@ module Thermos
|
|
11
13
|
end
|
12
14
|
|
13
15
|
def self.fill(key:, model:, deps: [], lookup_key: nil, &block)
|
14
|
-
|
15
|
-
|
16
|
+
BeverageStorage.instance.add_beverage(
|
17
|
+
Beverage.new(key: key, model: model, deps: deps, action: block, lookup_key: lookup_key)
|
18
|
+
)
|
16
19
|
end
|
17
20
|
|
18
21
|
def self.drink(key:, id:)
|
19
22
|
Rails.cache.fetch([key, id]) do
|
20
|
-
|
23
|
+
BeverageStorage.instance.get_beverage(key).action.call(id)
|
21
24
|
end
|
22
25
|
end
|
23
|
-
|
24
|
-
def self.empty
|
25
|
-
@thermos = {}
|
26
|
-
end
|
27
|
-
|
28
|
-
def self.refill_primary_caches(model)
|
29
|
-
@thermos.values.each do |beverage|
|
30
|
-
refill(beverage, model.send(beverage.lookup_key)) if beverage.model == model.class
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def self.refill_dependency_caches(model)
|
35
|
-
@thermos.values.each do |beverage|
|
36
|
-
deps = beverage.deps.select { |dependency| dependency.klass == model.class }
|
37
|
-
deps.each do |dependency|
|
38
|
-
beverage_models = beverage.model.joins(dependency.association).where(dependency.table => { id: model.id })
|
39
|
-
beverage_models.find_each do |beverage_model|
|
40
|
-
refill(beverage, beverage_model.send(beverage.lookup_key))
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def self.refill(beverage, id)
|
47
|
-
@thermos[beverage.key] = beverage
|
48
|
-
Rails.cache.write([beverage.key, id], beverage.action.call(id))
|
49
|
-
end
|
50
26
|
end
|
data/test/dummy/db/test.sqlite3
CHANGED
Binary file
|
@@ -1,51 +1,55 @@
|
|
1
|
-
[1m[36m (
|
2
|
-
[1m[35m (0.
|
3
|
-
[1m[36m (
|
4
|
-
[1m[35mActiveRecord::SchemaMigration Load (0.
|
1
|
+
[1m[36m (1.1ms)[0m [1mCREATE TABLE "schema_migrations" ("version" varchar NOT NULL) [0m
|
2
|
+
[1m[35m (0.1ms)[0m select sqlite_version(*)
|
3
|
+
[1m[36m (0.8ms)[0m [1mCREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")[0m
|
4
|
+
[1m[35mActiveRecord::SchemaMigration Load (0.1ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
5
5
|
Migrating to CreateCategories (20160325214744)
|
6
6
|
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
|
7
|
-
[1m[35m (0.
|
7
|
+
[1m[35m (0.3ms)[0m CREATE TABLE "categories" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar, "store_id" integer, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
|
8
8
|
[1m[36mSQL (0.2ms)[0m [1mINSERT INTO "schema_migrations" ("version") VALUES (?)[0m [["version", "20160325214744"]]
|
9
|
-
[1m[35m (0.
|
9
|
+
[1m[35m (0.7ms)[0m commit transaction
|
10
10
|
Migrating to CreateProducts (20160325214849)
|
11
11
|
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
|
12
12
|
[1m[35m (0.4ms)[0m CREATE TABLE "products" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
|
13
|
-
[1m[36mSQL (0.
|
14
|
-
[1m[35m (0.
|
13
|
+
[1m[36mSQL (0.1ms)[0m [1mINSERT INTO "schema_migrations" ("version") VALUES (?)[0m [["version", "20160325214849"]]
|
14
|
+
[1m[35m (0.6ms)[0m commit transaction
|
15
15
|
Migrating to CreateCategoryItems (20160325220006)
|
16
|
-
[1m[36m (0.
|
17
|
-
[1m[35m (0.
|
18
|
-
[1m[36mSQL (0.
|
16
|
+
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
17
|
+
[1m[35m (0.2ms)[0m CREATE TABLE "category_items" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar, "category_id" integer, "product_id" integer, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
|
18
|
+
[1m[36mSQL (0.1ms)[0m [1mINSERT INTO "schema_migrations" ("version") VALUES (?)[0m [["version", "20160325220006"]]
|
19
19
|
[1m[35m (0.7ms)[0m commit transaction
|
20
20
|
Migrating to CreateStores (20160326174530)
|
21
|
-
[1m[36m (0.
|
22
|
-
[1m[35m (
|
23
|
-
[1m[36mSQL (0.
|
24
|
-
[1m[35m (0.
|
21
|
+
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
22
|
+
[1m[35m (0.2ms)[0m CREATE TABLE "stores" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
|
23
|
+
[1m[36mSQL (0.1ms)[0m [1mINSERT INTO "schema_migrations" ("version") VALUES (?)[0m [["version", "20160326174530"]]
|
24
|
+
[1m[35m (0.6ms)[0m commit transaction
|
25
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
25
26
|
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
26
|
-
[1m[36mActiveRecord::SchemaMigration Load (0.5ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
27
27
|
[1m[35mActiveRecord::SchemaMigration Load (0.1ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
28
28
|
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
29
29
|
[1m[35mActiveRecord::SchemaMigration Load (0.1ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
30
30
|
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
31
31
|
[1m[35mActiveRecord::SchemaMigration Load (0.1ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
32
32
|
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
33
|
-
[1m[35mActiveRecord::SchemaMigration Load (0.
|
33
|
+
[1m[35mActiveRecord::SchemaMigration Load (0.1ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
34
34
|
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
35
35
|
[1m[35mActiveRecord::SchemaMigration Load (0.1ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
36
|
-
[1m[36mActiveRecord::SchemaMigration Load (0.
|
36
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
37
37
|
[1m[35mActiveRecord::SchemaMigration Load (0.1ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
38
38
|
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
39
|
+
[1m[35mActiveRecord::SchemaMigration Load (0.0ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
40
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
39
41
|
[1m[35mActiveRecord::SchemaMigration Load (0.1ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
40
42
|
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
41
43
|
[1m[35mActiveRecord::SchemaMigration Load (0.1ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
42
44
|
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
43
45
|
[1m[35mActiveRecord::SchemaMigration Load (0.1ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
44
46
|
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
45
|
-
[1m[35mActiveRecord::SchemaMigration Load (0.
|
47
|
+
[1m[35mActiveRecord::SchemaMigration Load (0.1ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
46
48
|
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
47
49
|
[1m[35mActiveRecord::SchemaMigration Load (0.1ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
48
50
|
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
51
|
+
[1m[35mActiveRecord::SchemaMigration Load (0.0ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
52
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
49
53
|
[1m[35mActiveRecord::SchemaMigration Load (0.1ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
50
54
|
[1m[36mActiveRecord::SchemaMigration Load (0.4ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
51
55
|
[1m[35mActiveRecord::SchemaMigration Load (0.1ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|