-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from mabeller/rake_install
Installation generator
- Loading branch information
Showing
8 changed files
with
82 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,79 @@ | ||
= Toastr | ||
|
||
Acts like standard cache, but kicks off a background job and serves stale data rather than blocking and recalculating. | ||
Acts like standard cache, but instead of blocking and recalculating, it serves stale data and kicks off a job to refresh it in the background. | ||
|
||
== INSTALLATION | ||
|
||
1. Install the gem | ||
```ruby | ||
gem 'toastr', github: 'drush/toastr' | ||
``` | ||
|
||
``` | ||
gem 'toastr', github: 'drush/toastr' | ||
``` | ||
|
||
2. Run the generator | ||
|
||
``` | ||
rails generate toastr:install | ||
``` | ||
|
||
3. Migrate your database | ||
|
||
``` | ||
rake db:migrate | ||
``` | ||
|
||
== USAGE | ||
|
||
1. Define an instance method on an ActiveRecord. | ||
2. After defining the method, declare ```has_toast :method_name``` | ||
2. After defining the method, declare | ||
```has_toast :method_name``` | ||
3. Three options for expiring the cached data are available, shown below | ||
|
||
```ruby | ||
class Oat < ActiveRecord::Base | ||
``` | ||
class Oat < ActiveRecord::Base | ||
has_many :related_records | ||
|
||
def breakfast | ||
sleep 2 | ||
{oat: :meal} | ||
end | ||
# by default only updates if this Oat instance is updated after the toast was calculated | ||
has_toast :breakfast | ||
def breakfast | ||
sleep 2 | ||
{oat: :meal} | ||
end | ||
# by default only updates if this Oat instance is updated after the toast was calculated | ||
has_toast :breakfast | ||
|
||
def daily_report | ||
sleep 2 | ||
{daily: :report} | ||
end | ||
# update if toast is older than 1 day | ||
has_toast :daily_report, expire_in: 1.day | ||
def daily_report | ||
sleep 2 | ||
{daily: :report} | ||
end | ||
# update if toast is older than 1 day | ||
has_toast :daily_report, expire_in: 1.day | ||
|
||
def arbitrary_expiration | ||
sleep 2 | ||
{arbitrary: :expiration} | ||
end | ||
# update on arbitrary block. example updates toast if any of its :related_records | ||
# have been updated since the toast was last updated | ||
has_toast :special, expire_if: -> (toast) { toast.parent.related_records.pluck(:updated_at).max > toast.updated_at } | ||
|
||
def arbitrary_expiration | ||
sleep 2 | ||
{arbitrary: :expiration} | ||
end | ||
# update on arbitrary block. example updates toast every time it's accessed if | ||
# parent object was created on 8th day of the month | ||
has_toast :special, expire_if: -> (toast) { toast.parent.created_at.day == 8 } | ||
|
||
end | ||
|
||
2.1.5 :001 > @oat = Oat.create | ||
=> #<Oat id: 1, created_at: "2015-06-09 23:13:41", updated_at: "2015-06-09 23:13:41"> | ||
2.1.5 :002 > @oat.breakfast | ||
=> {:error=>"Data not yet available"} | ||
2.1.5 :003 > @oat.breakfast # after waiting enough time for the toast to calculate | ||
=> {"oat"=>"meal", "toastr"=>{"elapsed"=>2.010508, "cached_at"=>"2015-06-09T16:14:25.701-07:00"}} | ||
``` | ||
``` | ||
|
||
``` | ||
2.1.5 :001 > @oat = Oat.create | ||
=> #<Oat id: 1, created_at: "2015-06-09 23:13:41", updated_at: "2015-06-09 23:13:41"> | ||
2.1.5 :002 > @oat.breakfast | ||
=> {:error=>"Data not yet available"} | ||
2.1.5 :003 > @oat.breakfast # after waiting enough time for the toast to calculate | ||
=> {"oat"=>"meal", "toastr"=>{"elapsed"=>2.010508, "cached_at"=>"2015-06-09T16:14:25.701-07:00"}} | ||
``` | ||
|
||
== TODO | ||
* Basic test coverage | ||
* Generator for migration | ||
* Extended test coverage | ||
* How to resolve conflicts between job_state and cache_state when errors occur | ||
|
||
== CHANGELOG | ||
* Installation generator | ||
* Basic test coverage | ||
* Adopt ActiveJob support instead of DJ | ||
|
||
This project rocks and uses MIT-LICENSE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,4 @@ def next_migration_number(dirname) | |
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,13 @@ | ||
class CreateToasts < ActiveRecord::Migration | ||
def self.up | ||
def change | ||
create_table :toastr_toasts, force: true do |t| | ||
t.references :parent, polymorphic: true | ||
t.string :category | ||
t.references :parent, polymorphic: true, null: false, index: true | ||
t.string :category, null: false | ||
t.text :cache_json | ||
t.text :status | ||
t.text :status, null: false | ||
t.timestamps null: true | ||
end | ||
|
||
# add_index :toasts, :status, name: 'toasts_status' | ||
add_index :toastr_toasts, [:parent_id, :parent_type, :category], name: 'toasts_parent_category' | ||
end | ||
|
||
def self.down | ||
drop_table :toastr_toasts | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class CreateToasts < ActiveRecord::Migration | ||
def change | ||
create_table :toastr_toasts, force: true do |t| | ||
t.references :parent, polymorphic: true, null: false, index: true | ||
t.string :category, null: false | ||
t.text :cache_json | ||
t.text :status, null: false | ||
t.timestamps null: true | ||
end | ||
|
||
add_index :toastr_toasts, [:parent_id, :parent_type, :category], name: 'toasts_parent_category' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters