thermos 0.0.4 → 0.0.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d6ebfef1211842802f2544583d0166fa06fa48ec
4
- data.tar.gz: 01dd7f14c0bf339d21f04c877799e7b00afc0cbe
3
+ metadata.gz: 3e56270eb773c627e1ecfec8ed6e1ba088a0811b
4
+ data.tar.gz: 96e2dbe3ea7c9d38b6e8367bc90e7342a38de677
5
5
  SHA512:
6
- metadata.gz: 5da371b9c7b85993ff137c8a835fd73b3dbb14b68d04a489ea6814cab85c0ac2b8f697cd02cf30ae7267d22c0737e5645deb8328a7fa6b8aa35bca56d1c4437c
7
- data.tar.gz: 77b08295b2f6e68d73946c1d9870502114e5bb85e0535311883e8e5d46962379d0bc198674f94651843256d54b8acec86b7e3517413e7e0bac7192ac26fa7136
6
+ metadata.gz: c32012da14be8ce3f8f481f6a4c534b9a18fa6f6e1af77592b913cd70df8831158d79dd7f8dd5bbb247de98d8cec99d51f5552cb1e31fec0701d9eef33965339
7
+ data.tar.gz: b4e7ed1511a64989d63b9c80f2b7a14f4cdbc43aac6e593d27ffc69e6516ef1be81d85519ab4d6310f34bf547ef70d3814f99fb41b550ee31e9d1c7bcaf7af01
@@ -1,32 +1,34 @@
1
- class Thermos::Beverage
2
- attr_reader :key, :model, :deps, :action, :lookup_key
1
+ module Thermos
2
+ class Beverage
3
+ attr_reader :key, :model, :deps, :action, :lookup_key
3
4
 
4
- def initialize(key:, model:, deps:, action:, lookup_key: nil)
5
- @key = key
6
- @model = model
7
- @lookup_key = lookup_key || :id
8
- @deps = deps.map do |dep|
9
- Thermos::Dependency.new(model: model, association: dep)
10
- end
11
- @action = action
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
- set_observers
14
- end
14
+ set_observers
15
+ end
15
16
 
16
- def deps_for_class(klass)
17
- @deps.select do |dep|
18
- dep.klass == klass.name
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
- private
23
+ private
23
24
 
24
- def set_observers
25
- observe(@model)
26
- @deps.each { |dep| observe(dep.klass) }
27
- end
25
+ def set_observers
26
+ observe(@model)
27
+ @deps.each { |dep| observe(dep.klass) }
28
+ end
28
29
 
29
- def observe(model)
30
- model.include(Thermos::Notifier) unless model.included_modules.include?(Thermos::Notifier)
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
@@ -1,11 +1,13 @@
1
- class Thermos::Dependency
2
- attr_reader :model, :association, :klass, :table
1
+ module Thermos
2
+ class Dependency
3
+ attr_reader :model, :association, :klass, :table
3
4
 
4
- def initialize(model:, association:)
5
- @model = model
6
- @association = association
7
- reflection = @model.reflections[@association.to_s]
8
- @table = reflection.table_name
9
- @klass = reflection.class_name.constantize
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
@@ -1,14 +1,15 @@
1
- module Thermos::Notifier
2
- extend ActiveSupport::Concern
1
+ module Thermos
2
+ module Notifier
3
+ extend ActiveSupport::Concern
3
4
 
4
- included do
5
- after_save :notify_thermos
6
- end
5
+ included do
6
+ after_save :notify_thermos
7
+ end
7
8
 
8
- private
9
+ private
9
10
 
10
- def notify_thermos
11
- Thermos::RefillJob.perform_later self
11
+ def notify_thermos
12
+ RefillJob.perform_later self
13
+ end
12
14
  end
13
-
14
15
  end
@@ -0,0 +1,8 @@
1
+ module Thermos
2
+ class RebuildCacheJob < ActiveJob::Base
3
+ def perform(key, id)
4
+ beverage = BeverageStorage.instance.get_beverage(key)
5
+ Rails.cache.write([key, id], beverage.action.call(id))
6
+ end
7
+ end
8
+ end
@@ -1,6 +1,28 @@
1
- class Thermos::RefillJob < ActiveJob::Base
2
- def perform(model)
3
- Thermos.refill_primary_caches(model)
4
- Thermos.refill_dependency_caches(model)
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
@@ -1,3 +1,3 @@
1
1
  module Thermos
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
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
- @thermos ||= {}
15
- @thermos[key] = Beverage.new(key: key, model: model, deps: deps, action: block, lookup_key: lookup_key)
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
- @thermos[key].action.call(id)
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
Binary file
@@ -1,51 +1,55 @@
1
-  (3.8ms) CREATE TABLE "schema_migrations" ("version" varchar NOT NULL) 
2
-  (0.2ms) select sqlite_version(*)
3
-  (1.4ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
4
- ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
1
+  (1.1ms) CREATE TABLE "schema_migrations" ("version" varchar NOT NULL) 
2
+  (0.1ms) select sqlite_version(*)
3
+  (0.8ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
4
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
5
5
  Migrating to CreateCategories (20160325214744)
6
6
   (0.1ms) begin transaction
7
-  (0.5ms) 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)
7
+  (0.3ms) 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
  SQL (0.2ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20160325214744"]]
9
-  (0.9ms) commit transaction
9
+  (0.7ms) commit transaction
10
10
  Migrating to CreateProducts (20160325214849)
11
11
   (0.1ms) begin transaction
12
12
   (0.4ms) CREATE TABLE "products" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
13
- SQL (0.3ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20160325214849"]]
14
-  (0.9ms) commit transaction
13
+ SQL (0.1ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20160325214849"]]
14
+  (0.6ms) commit transaction
15
15
  Migrating to CreateCategoryItems (20160325220006)
16
-  (0.1ms) begin transaction
17
-  (0.4ms) 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
- SQL (0.2ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20160325220006"]]
16
+  (0.0ms) begin transaction
17
+  (0.2ms) 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
+ SQL (0.1ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20160325220006"]]
19
19
   (0.7ms) commit transaction
20
20
  Migrating to CreateStores (20160326174530)
21
-  (0.1ms) begin transaction
22
-  (1.0ms) CREATE TABLE "stores" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
23
- SQL (0.2ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20160326174530"]]
24
-  (0.8ms) commit transaction
21
+  (0.0ms) begin transaction
22
+  (0.2ms) CREATE TABLE "stores" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
23
+ SQL (0.1ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20160326174530"]]
24
+  (0.6ms) commit transaction
25
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
25
26
  ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
26
- ActiveRecord::SchemaMigration Load (0.5ms) SELECT "schema_migrations".* FROM "schema_migrations"
27
27
  ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
28
28
  ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
29
29
  ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
30
30
  ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
31
31
  ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
32
32
  ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
33
- ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
33
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
34
34
  ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
35
35
  ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
36
- ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
36
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
37
37
  ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
38
38
  ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
39
+ ActiveRecord::SchemaMigration Load (0.0ms) SELECT "schema_migrations".* FROM "schema_migrations"
40
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
39
41
  ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
40
42
  ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
41
43
  ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
42
44
  ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
43
45
  ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
44
46
  ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
45
- ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
47
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
46
48
  ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
47
49
  ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
48
50
  ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
51
+ ActiveRecord::SchemaMigration Load (0.0ms) SELECT "schema_migrations".* FROM "schema_migrations"
52
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
49
53
  ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
50
54
  ActiveRecord::SchemaMigration Load (0.4ms) SELECT "schema_migrations".* FROM "schema_migrations"
51
55
  ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"