Class: Redify::Core
- Inherits:
-
Object
- Object
- Redify::Core
- Defined in:
- lib/redify/core.rb
Class Method Summary collapse
-
.after_create(record) ⇒ Object
Callbacks for Redified models.
- .after_destroy(record) ⇒ Object
- .after_save(record) ⇒ Object
-
.fetch_record(options) ⇒ Object
Valid options: :resource - ActiveRecord model class (User) :id - record primary key.
-
.fetch_records_by_edge(resource, last = false, count = 1) ⇒ Object
fetch first or last N entries.
-
.flush_keys(resource = nil) ⇒ Object
Deletes all Redis keys under current namespace.
-
.get_key_name_for(options) ⇒ Object
Returns the key name without the namespace.
- .get_key_name_for_collection(options) ⇒ Object
- .get_key_name_for_record(record) ⇒ Object
- .get_key_name_for_resource(resource) ⇒ Object
- .get_key_name_for_resource_and_id(options) ⇒ Object
-
.get_namespaced_key_name_for(options) ⇒ Object
Valid options: :resource - ActiveRecord model class (User) :collection - camel-cased string denoting collection name :record - a single record instance (User.find 1).
-
.initialize_resource(resource) ⇒ Object
Loads all records into Redis upon initialization.
- .redify_all_records(resource, redis) ⇒ Object
-
.redify_record(record) ⇒ Object
Turns an ActiveRecord instance into a Redis entry.
Class Method Details
.after_create(record) ⇒ Object
Callbacks for Redified models
110 111 112 |
# File 'lib/redify/core.rb', line 110 def after_create(record) record.class.redify_handler.rpush record.class.redify_keys[:all], record.id end |
.after_destroy(record) ⇒ Object
118 119 120 121 |
# File 'lib/redify/core.rb', line 118 def after_destroy(record) puts "after_destroy" puts record.inspect end |
.after_save(record) ⇒ Object
114 115 116 |
# File 'lib/redify/core.rb', line 114 def after_save(record) redify_record record end |
.fetch_record(options) ⇒ Object
Valid options:
:resource - ActiveRecord model class (User)
:id - record primary key
52 53 54 55 56 57 58 59 60 |
# File 'lib/redify/core.rb', line 52 def fetch_record() resource = [:resource] return nil unless resource return resource.find([:id], :skip_redify => true) unless resource.redify_handler # Redis is unavailable return synchronize(:resource => resource, :id => [:id]) end |
.fetch_records_by_edge(resource, last = false, count = 1) ⇒ Object
fetch first or last N entries
63 64 65 66 67 |
# File 'lib/redify/core.rb', line 63 def fetch_records_by_edge(resource, last = false, count = 1) # TODO implement COUNT edge_id = redify_handler.lindex resource.redify_keys[:all], (last ? -1 : 0) edge_id ? Redify::Core.fetch_record(:resource => resource, :id => edge_id) : nil end |
.flush_keys(resource = nil) ⇒ Object
Deletes all Redis keys under current namespace
124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/redify/core.rb', line 124 def flush_keys(resource = nil) namespace = Redify::Settings.namespace redis = Redify::Settings.default_handler pattern = "#{namespace}:" pattern << "#{resource}" if resource pattern << "*" redis.keys(pattern).each do |key| redis.del(key) end # namespace is also a key by itself redis.del(namespace) unless resource end |
.get_key_name_for(options) ⇒ Object
Returns the key name without the namespace.
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/redify/core.rb', line 16 def get_key_name_for() if [:collection] get_key_name_for_collection() elsif [:record] get_key_name_for_record([:record]) elsif [:resource] && [:id] get_key_name_for_resource_and_id() elsif [:resource] get_key_name_for_resource([:resource]) end end |
.get_key_name_for_collection(options) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/redify/core.rb', line 28 def get_key_name_for_collection() key_name = get_key_name_for_resource([:resource]) if [:collection] == "All" key_name << ":All" else key_name << ":Collections:#{[:collection]}" end end |
.get_key_name_for_record(record) ⇒ Object
37 38 39 |
# File 'lib/redify/core.rb', line 37 def get_key_name_for_record(record) "#{record.class.name}:#{record.id}" end |
.get_key_name_for_resource(resource) ⇒ Object
41 42 43 |
# File 'lib/redify/core.rb', line 41 def get_key_name_for_resource(resource) "#{ActiveSupport::Inflector.pluralize(resource.to_s)}" end |
.get_key_name_for_resource_and_id(options) ⇒ Object
45 46 47 |
# File 'lib/redify/core.rb', line 45 def get_key_name_for_resource_and_id() "#{[:resource].to_s}:#{[:id]}" end |
.get_namespaced_key_name_for(options) ⇒ Object
Valid options:
:resource - ActiveRecord model class (User)
:collection - camel-cased string denoting collection name
:record - a single record instance (User.find 1)
9 10 11 12 13 |
# File 'lib/redify/core.rb', line 9 def get_namespaced_key_name_for() key_name = get_key_name_for() namespace = Redify::Settings.namespace namespace ? "#{namespace}:#{key_name}" : key_name end |
.initialize_resource(resource) ⇒ Object
Loads all records into Redis upon initialization
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/redify/core.rb', line 70 def initialize_resource(resource) redis = resource.redify_handler resource_status = redis.hget(resource.redify_keys[:resource], 'status') if resource_status == "READY" puts "[Redify] Resource #{resource} is already loaded." elsif resource_status == 'LOADING' puts "[Redify] Resource #{resource} is being loaded by another process." else puts "[Redify] Initializing resource #{resource}" redify_all_records(resource, redis) puts "... finished" end end |
.redify_all_records(resource, redis) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/redify/core.rb', line 87 def redify_all_records(resource, redis) flush_keys(resource) resource_key = resource.redify_keys[:resource] redis.hset(resource_key, 'status', 'LOADING') resource.find(:all, :skip_redify => true).each do |r| redify_record(r) redis.rpush(redify_keys[:all], r.id) end # TODO load associations and scopes redis.hset(resource_key, 'status', 'READY') end |
.redify_record(record) ⇒ Object
Turns an ActiveRecord instance into a Redis entry. Updates Redis entry if it already exists.
101 102 103 104 105 106 |
# File 'lib/redify/core.rb', line 101 def redify_record(record) key = Redify::Core.get_namespaced_key_name_for(:record => record) record.class.redify_handler.hmset(key, record.attributes.to_a.flatten) puts "[Redify] #{record.class}\##{record.id} redified" return record end |