forked from pictrs/fog-backblaze
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token_cache.rb
131 lines (110 loc) · 2.53 KB
/
token_cache.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Each request must have authentication header, recieved from b2_authorize_account
# Authentication token is active for 24 hours, same as uploadUrl. To make things faster we keep it in cache
#
# Avaliable cache storages: file, memory, null
#
# To create own cache storage (stored in redis for example)
#
# class RedisTokenCache < Fog::Backblaze::TokenCache
# def initialize(redis_url)
# @redis = Redis.new(redis_url)
# super()
# end
# def load_data
# raw_data = @redis.get("b2_token_cache")
# raw_data ? ::JSON.parse(raw_data) : {}
# end
# def save_data
# @redis.set("b2_token_cache", JSON.pretty_generate(@data))
# end
# end
#
# Fog::Storage.new(provider: 'backblaze', ..., token_cache: RedisTokenCache.new)
#
require 'json'
class Fog::Backblaze::TokenCache
def initialize
@data = load_data || {}
end
def load_data
end
def save_data
end
TTLS = {
auth_response: 3600 * 24,
buckets: 3600 * 24,
upload_url: 3600 * 24
}
def fetch(field)
if result = access_part(field)
result
else
result = yield
write_part(field, result, TTLS[field])
save_data
result
end
end
def auth_response
access_part(:auth_response)
end
def auth_response=(value)
write_part(:auth_response, value, 3600 * 24)
save_data
end
def buckets
access_part(:buckets)
end
def buckets=(value)
write_part(:buckets, value, 3600 * 24)
save_data
end
def upload_url
end
def access_part(name)
name = name.to_s
if @data[name] && ::DateTime.parse(@data[name]['expires_at']).to_time > ::Time.now
@data[name]['value']
end
end
def write_part(name, value, ttl = 3600)
ttl = 3600 if ttl.nil?
name = name.to_s
if value.nil?
@data.delete(name)
else
@data[name] = {
'value' => value,
'expires_at' => ::Time.at(::Time.now + ttl - 1).to_s
}
end
end
def reset
@data = {}
save_data
end
# stored in local file
class FileTokenCache < Fog::Backblaze::TokenCache
def initialize(file)
@file = file
super()
end
def load_data
if File.exist?(@file)
::JSON.parse(File.open(@file, 'rb', &:read))
else
{}
end
end
def save_data
File.open(@file, 'wb') do |f|
f.write(::JSON.pretty_generate(@data) + "\n")
end
end
end
# black hole, always clean cache
class NullTokenCache < Fog::Backblaze::TokenCache
def write_part(name, value, ttl = 3600)
end
end
end