Module: Monocle

Extended by:
ActiveSupport::Concern
Defined in:
lib/monocle.rb,
lib/monocle/server.rb,
lib/monocle/version.rb

Defined Under Namespace

Modules: ClassMethods Classes: Server

Constant Summary collapse

VERSION =
'0.2.2'

Instance Method Summary collapse

Instance Method Details

#cache_field_for_view(view_type) ⇒ Object



93
94
95
# File 'lib/monocle.rb', line 93

def cache_field_for_view(view_type)
  :"#{view_type}_views"
end

#cache_view_count(view_type, count) ⇒ Object



105
106
107
108
# File 'lib/monocle.rb', line 105

def cache_view_count(view_type, count)
  update_column(cache_field_for_view(view_type), count) if respond_to?(:update_column)
  set(cache_field_for_view(view_type), count) if respond_to?(:set)
end

#destroy_viewsObject



110
111
112
# File 'lib/monocle.rb', line 110

def destroy_views
  self._monocle_redis_connection.del(self.class.monocle_key(id))
end

#should_cache_view_count?Boolean

Returns:

  • (Boolean)


97
98
99
100
101
102
103
# File 'lib/monocle.rb', line 97

def should_cache_view_count?
  if self._monocle_options[:cache_view_counts]
    self.send(self._monocle_options[:cache_threshold_check_field]) < (Time.now - self._monocle_options[:cache_threshold])
  else
    false
  end
end

#view!Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/monocle.rb', line 75

def view!
  results = self._monocle_redis_connection.pipelined do
    self._monocle_view_types.keys.each do |view_type|
      self._monocle_redis_connection.hincrby(self.class.monocle_key(id), self.send("#{view_type}_views_field"), 1)
    end
    self._monocle_redis_connection.zadd(self.class.monocle_key('recently_viewed'), Time.now.to_i, id)
    self._monocle_redis_connection.zincrby(self.class.monocle_key('view_counts'), 1, id)
  end

  if should_cache_view_count?
    self._monocle_view_types.keys.each_with_index do |view_type, i|
      cache_view_count(view_type, results[i])
    end
    self.update_column(self._monocle_options[:cache_threshold_check_field].to_sym, Time.now) if respond_to?(:update_column)
    self.set(self._monocle_options[:cache_threshold_check_field].to_sym, Time.now) if respond_to?(:set)
  end
end