Class: JobParser::Cache::MongoStore
- Inherits:
-
Object
- Object
- JobParser::Cache::MongoStore
- Defined in:
- lib/jobparser/cache/mongostore.rb
Defined Under Namespace
Classes: Job
Instance Method Summary collapse
- #cache_expired?(url) ⇒ Boolean
- #clear_all ⇒ Object
- #get(url) ⇒ Object
- #has_cache_for_url?(url) ⇒ Boolean
- #store(hash) ⇒ Object
- #view_cache ⇒ Object
Instance Method Details
#cache_expired?(url) ⇒ Boolean
16 17 18 19 20 |
# File 'lib/jobparser/cache/mongostore.rb', line 16 def cache_expired?(url) job = job_for_url(url).first expire_time = (job.created_at + JobParser.config[:cache_expire]) Time.now > expire_time end |
#clear_all ⇒ Object
32 33 34 |
# File 'lib/jobparser/cache/mongostore.rb', line 32 def clear_all MongoStore::Job.each(&:delete) end |
#get(url) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/jobparser/cache/mongostore.rb', line 22 def get(url) job = job_for_url(url).first {}.tap do |job_obj| job.attributes.each do |k, v| job_obj[k.to_sym] = v unless %w{created_at _id updated_at}.include?(k) end job_obj[:from_cache] = true end end |
#has_cache_for_url?(url) ⇒ Boolean
6 7 8 |
# File 'lib/jobparser/cache/mongostore.rb', line 6 def has_cache_for_url?(url) job_for_url(url).count > 0 end |
#store(hash) ⇒ Object
10 11 12 13 14 |
# File 'lib/jobparser/cache/mongostore.rb', line 10 def store(hash) job_for_url(hash[:url]).delete hash = strip_fields_not_stored(hash) Job.create(hash) end |
#view_cache ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/jobparser/cache/mongostore.rb', line 36 def view_cache [].tap do |res| Job.each do |job| res.push({ :url => job.url, :created => job.created_at }) end end end |