Class: FlyAdmin::PageCache
- Inherits:
-
Object
- Object
- FlyAdmin::PageCache
- Defined in:
- lib/fly_admin/page_cache.rb
Constant Summary collapse
- EXPIRATION_TIMEOUT =
10.minutes
Instance Attribute Summary collapse
-
#rails_cache ⇒ Object
Returns the value of attribute rails_cache.
Instance Method Summary collapse
- #check_footer(elem) ⇒ Object
- #clear ⇒ Object
-
#clear_element(elem) ⇒ Object
After saving footer, clear cache and check if from imbs source(now - from db).
- #get_footer_from_bornpay(footer, name, paysite, country) ⇒ Object
- #get_footer_from_imbs(footer, name, paysite, country) ⇒ Object
- #http_load(name, paysite, country) ⇒ Object
-
#initialize ⇒ PageCache
constructor
A new instance of PageCache.
-
#read(elem) ⇒ Object
Get footers like this footer_wap_ru Get pages (unsubscribe etc) like this page_mt_en.
- #save_footer_to_db(footer_text, name, paysite, country) ⇒ Object
- #write(name, text) ⇒ Object
- #write_with_date(elem, text) ⇒ Object
Constructor Details
#initialize ⇒ PageCache
Returns a new instance of PageCache.
8 9 10 |
# File 'lib/fly_admin/page_cache.rb', line 8 def initialize @rails_cache = ActiveSupport::Cache::MemoryStore.new end |
Instance Attribute Details
#rails_cache ⇒ Object
Returns the value of attribute rails_cache.
4 5 6 |
# File 'lib/fly_admin/page_cache.rb', line 4 def rails_cache @rails_cache end |
Instance Method Details
#check_footer(elem) ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'lib/fly_admin/page_cache.rb', line 57 def (elem) name, paysite, country = elem.split '_' = FlyAdmin::Footer .joins(:country, :paysite) .where('fly_admin_countries.name = ? and fly_admin_paysites.name = ?', country, paysite).first # get footer from db , else get it content from url like this www.bornpay.com/footer/hsSDAasd.wap.ru.html FlyAdmin.api_type == 'bornpay' ? (, name, paysite, country) : (, name, paysite, country) end |
#clear ⇒ Object
53 54 55 |
# File 'lib/fly_admin/page_cache.rb', line 53 def clear @rails_cache.clear end |
#clear_element(elem) ⇒ Object
After saving footer, clear cache and check if from imbs source(now - from db)
38 39 40 41 42 |
# File 'lib/fly_admin/page_cache.rb', line 38 def clear_element(elem) @rails_cache.delete_matched(elem) new_text = (elem) write_with_date(elem, new_text) end |
#get_footer_from_bornpay(footer, name, paysite, country) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/fly_admin/page_cache.rb', line 66 def (, name, paysite, country) if && .local? # Get from db .send(name) else # Load from bornpay = http_load(name, paysite, country) # Save bornpay footer to db (, name, paysite, country) return end end |
#get_footer_from_imbs(footer, name, paysite, country) ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/fly_admin/page_cache.rb', line 79 def (, name, paysite, country) if # Get from db .send(name) else # Empty footer "" end end |
#http_load(name, paysite, country) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/fly_admin/page_cache.rb', line 89 def http_load(name, paysite, country) name = (name == 'content') ? 'footer' : name service = SiteConfig['service_code'] link = "/#{name}/#{service}.#{paysite}.#{country}.html" uri = URI("http://www.bornpay.com#{link}") res = Net::HTTP.get_response(uri) = "#{res.code} , message: #{res.}, url: http://bornpay.com#{link}" API_LOG.info "GET FlyAdmin::Footer : #{} " if res.code == '200' && res. == 'OK' res.body.force_encoding("UTF-8") else "" end end |
#read(elem) ⇒ Object
Get footers like this footer_wap_ru Get pages (unsubscribe etc) like this page_mt_en
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/fly_admin/page_cache.rb', line 15 def read(elem) text = @rails_cache.read(elem) expired_in = @rails_cache.read("#{elem}_expired_an") unless text text = (elem) # Write to Rails cache like # elem = content_wap_ru write_with_date(elem, text) else if Time.now > expired_in new_text = (elem) unless new_text.empty? @rails_cache.clear write_with_date(elem, new_text) return new_text end end end text end |
#save_footer_to_db(footer_text, name, paysite, country) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/fly_admin/page_cache.rb', line 104 def (, name, paysite, country) if .present? API_LOG.info "footer :#{}, name: #{name}, paysite: #{paysite}, country: #{country}" country = Country.find_by_name(country) paysite = Paysite.find_by_name(paysite) case name when 'content' attributes = { 'content' => } when 'unsubscription' attributes = { 'unsubscription' => } when 'offer' attributes = { 'offer' => } when 'terms' attributes = { 'terms' => } end attributes.merge!('source_type' => FlyAdmin.api_type) = FlyAdmin::Footer.find_or_create_by(country: country, paysite: paysite) .assign_attributes(attributes) .source_type = FlyAdmin.api_type .save! end end |
#write(name, text) ⇒ Object
49 50 51 |
# File 'lib/fly_admin/page_cache.rb', line 49 def write(name, text) @rails_cache.write(name, text) end |
#write_with_date(elem, text) ⇒ Object
44 45 46 47 |
# File 'lib/fly_admin/page_cache.rb', line 44 def write_with_date(elem, text) write(elem, text) write("#{elem}_expired_an", Time.now + EXPIRATION_TIMEOUT) end |