Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Léo-Paul Géneau
gitlab-ce
Commits
156788ba
Commit
156788ba
authored
Dec 19, 2018
by
Douglas Barbosa Alexandre
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Port Gitlab::JsonCache to CE
parent
806ea576
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
488 additions
and
0 deletions
+488
-0
lib/gitlab/json_cache.rb
lib/gitlab/json_cache.rb
+87
-0
spec/lib/gitlab/json_cache_spec.rb
spec/lib/gitlab/json_cache_spec.rb
+401
-0
No files found.
lib/gitlab/json_cache.rb
0 → 100644
View file @
156788ba
# frozen_string_literal: true
module
Gitlab
class
JsonCache
attr_reader
:backend
,
:cache_key_with_version
,
:namespace
def
initialize
(
options
=
{})
@backend
=
options
.
fetch
(
:backend
,
Rails
.
cache
)
@namespace
=
options
.
fetch
(
:namespace
,
nil
)
@cache_key_with_version
=
options
.
fetch
(
:cache_key_with_version
,
true
)
end
def
active?
if
backend
.
respond_to?
(
:active?
)
backend
.
active?
else
true
end
end
def
cache_key
(
key
)
expanded_cache_key
=
[
namespace
,
key
].
compact
if
cache_key_with_version
expanded_cache_key
<<
Rails
.
version
end
expanded_cache_key
.
join
(
':'
)
end
def
expire
(
key
)
backend
.
delete
(
cache_key
(
key
))
end
def
read
(
key
,
klass
=
nil
)
value
=
backend
.
read
(
cache_key
(
key
))
value
=
parse_value
(
value
,
klass
)
if
value
value
end
def
write
(
key
,
value
,
options
=
nil
)
backend
.
write
(
cache_key
(
key
),
value
.
to_json
,
options
)
end
def
fetch
(
key
,
options
=
{},
&
block
)
klass
=
options
.
delete
(
:as
)
value
=
read
(
key
,
klass
)
return
value
unless
value
.
nil?
value
=
yield
write
(
key
,
value
,
options
)
value
end
private
def
parse_value
(
raw
,
klass
)
value
=
ActiveSupport
::
JSON
.
decode
(
raw
)
case
value
when
Hash
then
parse_entry
(
value
,
klass
)
when
Array
then
parse_entries
(
value
,
klass
)
else
value
end
rescue
ActiveSupport
::
JSON
.
parse_error
nil
end
def
parse_entry
(
raw
,
klass
)
klass
.
new
(
raw
)
if
valid_entry?
(
raw
,
klass
)
end
def
valid_entry?
(
raw
,
klass
)
return
false
unless
klass
&&
raw
.
is_a?
(
Hash
)
(
raw
.
keys
-
klass
.
attribute_names
).
empty?
end
def
parse_entries
(
values
,
klass
)
values
.
map
{
|
value
|
parse_entry
(
value
,
klass
)
}.
compact
end
end
end
spec/lib/gitlab/json_cache_spec.rb
0 → 100644
View file @
156788ba
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment