Class: Rails::Ding::CacheService

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/ding/cache_service.rb

Class Method Summary collapse

Class Method Details

.get(key) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rails/ding/cache_service.rb', line 44

def self.get(key)
  if key.present?
    data = get_file(Rails.root.to_s + "config/filecache.yml")
    if data.present? && data.has_key?(key)
        item = data["#{key}"]
        return false  if !item
        if item['expire_time']>0 && item['expire_time'] < Time.now.to_i
            return false;
        end
        return item["#{key}"]
    else
      return false;
    end
  end
end

.get_file(filename) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/rails/ding/cache_service.rb', line 60

def self.get_file(filename)
  if !File.exist?(filename)
    file = File.open(filename, "w")
    file.write("{}")
    file.close
  end
  return YAML.load(File.read(filename))
end

.get_value(name) ⇒ Object

使用变量



26
27
28
# File 'lib/rails/ding/cache_service.rb', line 26

def self.get_value(name)
  get(name)
end

.getCorpAccessTokenObject



17
18
19
# File 'lib/rails/ding/cache_service.rb', line 17

def self.getCorpAccessToken
  return get("corp_access_token")
end

.getJsTicketObject



9
10
11
# File 'lib/rails/ding/cache_service.rb', line 9

def self.getJsTicket()
  return get("js_ticket")
end

.set(key, value, options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rails/ding/cache_service.rb', line 30

def self.set(key,value,options = {})
  ex = options[:ex] ? Time.now.to_i + options[:ex] : 0
  if key && value
    data = get_file(Rails.root.to_s + "config/filecache.yml")

    item = {}
    item["#{key}"] = value
    item['expire_time'] = ex
    item['create_time'] = Time.now.to_i
    data["#{key}"] = item
    set_file(Rails.root.to_s + "config/filecache.yml",data.to_json)
  end
end

.set_file(filename, content) ⇒ Object



69
70
71
72
73
# File 'lib/rails/ding/cache_service.rb', line 69

def self.set_file(filename, content)
  file = File.open(filename, "w")
  file.write(content)
  file.close unless file.nil?
end

.set_value(name, value) ⇒ Object

保存变量



22
23
24
# File 'lib/rails/ding/cache_service.rb', line 22

def self.set_value(name,value)
  set(name, value)
end

.setCorpAccessToken(accessToken) ⇒ Object



13
14
15
# File 'lib/rails/ding/cache_service.rb', line 13

def self.setCorpAccessToken(accessToken)
  set("corp_access_token", accessToken, ex: 7000) # corp access token有效期为7200秒,这里设置为7000秒
end

.setJsTicket(ticket) ⇒ Object



5
6
7
# File 'lib/rails/ding/cache_service.rb', line 5

def self.setJsTicket(ticket)
  set("js_ticket", ticket, ex: 7000); # js ticket有效期为7200秒,这里设置为7000秒
end