Module: LionAttr

Extended by:
ModuleInterface
Defined in:
lib/lion_attr.rb,
lib/lion_attr/config.rb,
lib/lion_attr/version.rb,
lib/lion_attr/redis_pool.rb,
lib/lion_attr/internal_redis.rb

Overview

LionAttr, a gem to store Mongoid object in Redis for fast access. It also helps you to store object attributes to real time update via Redis increamental operations.

In many web applications, updating objects to database (mongodb) too often would hurt overal application performance because databases usually need to do some extra tasks such as indexing, updating its cache …

One of the most popular example for this problem is tracking pageview of a web page. Imagine if you have a blog and you want to know how many time people visit a specific article, what would you do? An obvious way to do so is whenever a person make a request, you increase the record holding pageview counter by using ‘mongoid#inc` method. Doing this is not a good idea because it increases database calls making your application slower.

A better solution for this problem is to keep the pageview counter in memory (which is fast) key-value storage such as Redis. Instead of increasing the counter by the database, we increase the counter inside Redis and save back to the database later (like after 10 mins, 30 mins or even a day).

LionAttr provides APIs for you to do so with ease.

That counter is usually an attribute of a Model object, the difference is that attribute will get the value from Redis instead of the database. We call it live attribute because it tends update its value very frequently.

@todo: Cache object @todo: Custom key @todo: Inc @todo: Callbacks @todo: Configure

Examples:


class Article
  include Mongoid::Document
  include LionAttr
  field :url, type: String
  field :view, type: Integer

  # field :view will be stored in Redis and saved back to Mongodb later
  live :view
end

See Also:

Since:

  • 0.1.0

Defined Under Namespace

Modules: ClassMethods, Config, ModuleInterface Classes: InternalRedis, RedisPool

Constant Summary collapse

VERSION =

Since:

  • 0.1.0

"0.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ModuleInterface

configure

Class Method Details

.included(base) ⇒ Object

Including LionAttr will set an after save callback to update the object cache in Redis.

Parameters:

  • base

    Mongoid::Document

Since:

  • 0.1.0



59
60
61
62
# File 'lib/lion_attr.rb', line 59

def self.included(base)
  base.extend(ClassMethods)
  base.set_callback(:save, :after, :update_to_redis, prepend: true)
end

Instance Method Details

#clean_cache_after_destroyObject

Call this method will clear all of Redis keys related to the object.

Since:

  • 0.1.0



93
94
95
96
97
98
# File 'lib/lion_attr.rb', line 93

def clean_cache_after_destroy
  @live_keys ||= self.class.live_fields.map { |f| key(f) }
  @internal_redis ||= InternalRedis.new(self.class.name)
  @internal_redis.del @live_keys
  @internal_redis.del id
end

#key(field) ⇒ String

Key to store in Redis, it is combined by object identity and field name.

Examples:

article.key(:view)
#=> 54d5f10d5675730bd1050000_view

article.url = "http://clicklion.com/article/1"
article.class.live_key = :url
article.key(:view)
#=> http://clicklion.com/article/1_view

Parameters:

  • field (String, Symbol)

Returns:

  • (String)

See Also:

Since:

  • 0.1.0



78
79
80
# File 'lib/lion_attr.rb', line 78

def key(field)
  self.class._key(self.send(self.class.live_key), field)
end

#update_to_redisObject

Call this method to manually create a cache of the object in Redis. It will store the object as a json string in a Redis Hash with key is the object’s id. Object from different classes will be stored in different hashes distinguished by class full name.

See Also:

Since:

  • 0.1.0



88
89
90
# File 'lib/lion_attr.rb', line 88

def update_to_redis
  InternalRedis.new(self.class.name).set(id, as_document.to_json)
end