Class: Jpnstacapi::RailsCache

Inherits:
Api
  • Object
show all
Defined in:
lib/jpnstacapi/rails_cache.rb

Instance Method Summary collapse

Methods inherited from Api

#getXml, #httpRequest, #initialize, #setParameters

Constructor Details

This class inherits a constructor from Jpnstacapi::Api

Instance Method Details

#is_enable?(key) ⇒ Boolean

Returns:

  • (Boolean)


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/jpnstacapi/rails_cache.rb', line 2

def is_enable? key
  require 'digest/md5'
  path = Rails.root.join('tmp/jpnstacapi')
  path = path.join(Digest::MD5.hexdigest(key))
  unless File.exists? path
    return false
  end
  cachedate = File.mtime(path)
  yesterday = Time.now - 60 * 60 * 24
  if cachedate < yesterday
    return false
  else
    cachefile = File.open(path)
    cache = cachefile.read
    cachefile.close
    return cache
  end
end

#save(key, source) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/jpnstacapi/rails_cache.rb', line 21

def save key, source
  require 'digest/md5'
  path = Rails.root.join('tmp/jpnstacapi')
  unless File.directory? path
    FileUtils.mkdir_p path
  end
  unless File.writable? path
    return false
  end
  detected_encoding = source.encoding
  unless detected_encoding == Encoding::UTF_8
    source = source
      .encode(detected_encoding)
      .force_encoding Encoding::UTF_8
  end
  File.open path.join(Digest::MD5.hexdigest(key)), 'w' do |f|
    f.write source
  end
end