Skip to content

Commit fe1b6da

Browse files
author
root
committed
Add a Cache::Adapter class to handle different caching system :)
1 parent 6049fc4 commit fe1b6da

File tree

5 files changed

+107
-53
lines changed

5 files changed

+107
-53
lines changed

Diff for: lib/redmine_git_hosting/cache.rb

+13-51
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,17 @@ def max_cache_size
2020

2121

2222
def set_cache(repo_id, out_value, primary_key, secondary_key = nil)
23-
logger.debug("Inserting cache entry for repository '#{repo_id}'")
24-
logger.debug(compose_key(primary_key, secondary_key))
2523
command = compose_key(primary_key, secondary_key)
26-
set_cache_entry(command, out_value, repo_id)
24+
adapter.apply_cache_limit(max_cache_elements) if adapter.set_cache(command, out_value, repo_id)
2725
end
2826

2927

30-
def check_cache(primary_key, secondary_key = nil)
31-
cached = get_cache_entry(primary_key, secondary_key)
28+
def get_cache(primary_key, secondary_key = nil)
29+
command = compose_key(primary_key, secondary_key)
30+
cached = adapter.get_cache(command)
3231

3332
if cached
34-
if valid_cache_entry?(cached)
33+
if valid_cache_entry?(cached.created_at)
3534
# Update updated_at flag
3635
cached.touch unless cached.command_output.nil?
3736
out = cached.command_output
@@ -52,51 +51,31 @@ def check_cache(primary_key, secondary_key = nil)
5251
def clear_obsolete_cache_entries
5352
return if max_cache_time < 0 # No expiration needed
5453
limit = Time.now - max_cache_time
55-
do_clear_obsolete_cache_entries(limit)
54+
adapter.clear_obsolete_cache_entries(limit)
5655
end
5756

5857

5958
# Clear the cache entries for given repository / git_cache_id
6059
def clear_cache_for_repository(repo_id)
61-
do_clear_cache_for_repository(repo_id)
60+
adapter.clear_cache_for_repository(repo_id)
6261
end
6362

6463

65-
private
66-
67-
68-
def set_cache_entry(command, output, repo_id)
69-
begin
70-
GitCache.create(
71-
command: command,
72-
command_output: output,
73-
repo_identifier: repo_id
74-
)
75-
rescue => e
76-
logger.error("Could not insert in cache, this is the error : '#{e.message}'")
77-
else
78-
apply_cache_limit
79-
end
80-
end
64+
def adapter
65+
@adapter ||= Cache::Adapter.factory
66+
end
8167

8268

83-
def get_cache_entry(primary_key, secondary_key)
84-
GitCache.find_by_command(compose_key(primary_key, secondary_key))
85-
end
69+
private
8670

8771

88-
def valid_cache_entry?(cached)
72+
def valid_cache_entry?(cached_entry_date)
8973
current_time = ActiveRecord::Base.default_timezone == :utc ? Time.now.utc : Time.now
90-
expired = (current_time.to_i - cached.created_at.to_i < max_cache_time)
74+
expired = (current_time.to_i - cached_entry_date.to_i > max_cache_time)
9175
(!expired || max_cache_time < 0) ? true : false
9276
end
9377

9478

95-
def apply_cache_limit
96-
GitCache.find(:last, order: 'created_at DESC').destroy if max_cache_elements >= 0 && GitCache.count > max_cache_elements
97-
end
98-
99-
10079
def compose_key(key1, key2)
10180
if key2 && !key2.blank?
10281
key1 + "\n" + key2
@@ -105,23 +84,6 @@ def compose_key(key1, key2)
10584
end
10685
end
10786

108-
109-
def do_clear_obsolete_cache_entries(limit)
110-
deleted = GitCache.delete_all(['created_at < ?', limit])
111-
logger.info("Removed '#{deleted}' expired cache entries among all repositories")
112-
end
113-
114-
115-
def do_clear_cache_for_repository(repo_id)
116-
deleted = GitCache.delete_all(['repo_identifier = ?', repo_id])
117-
logger.info("Removed '#{deleted}' expired cache entries for repository '#{repo_id}'")
118-
end
119-
120-
121-
def logger
122-
RedmineGitHosting.logger
123-
end
124-
12587
end
12688

12789
end

Diff for: lib/redmine_git_hosting/cache/abstract_cache.rb

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module RedmineGitHosting::Cache
2+
class AbstractCache
3+
4+
def set_cache(command, output, repo_id)
5+
raise NotImplementedError
6+
end
7+
8+
9+
def get_cache(command)
10+
raise NotImplementedError
11+
end
12+
13+
14+
def clear_obsolete_cache_entries(limit)
15+
raise NotImplementedError
16+
end
17+
18+
19+
def clear_cache_for_repository(repo_id)
20+
raise NotImplementedError
21+
end
22+
23+
24+
def apply_cache_limit(max_cache_elements)
25+
raise NotImplementedError
26+
end
27+
28+
29+
private
30+
31+
32+
def logger
33+
RedmineGitHosting.logger
34+
end
35+
36+
end
37+
end

Diff for: lib/redmine_git_hosting/cache/adapter.rb

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module RedmineGitHosting::Cache
2+
class Adapter
3+
4+
class << self
5+
6+
def factory
7+
Database.new
8+
end
9+
10+
end
11+
12+
end
13+
end

Diff for: lib/redmine_git_hosting/cache/database.rb

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module RedmineGitHosting::Cache
2+
class Database < AbstractCache
3+
4+
def set_cache(command, output, repo_id)
5+
logger.debug("Inserting cache entry for repository '#{repo_id}'")
6+
begin
7+
GitCache.create(
8+
command: command,
9+
command_output: output,
10+
repo_identifier: repo_id
11+
)
12+
true
13+
rescue => e
14+
logger.error("Could not insert in cache, this is the error : '#{e.message}'")
15+
false
16+
end
17+
end
18+
19+
20+
def get_cache(command)
21+
GitCache.find_by_command(command)
22+
end
23+
24+
25+
def clear_obsolete_cache_entries(limit)
26+
deleted = GitCache.delete_all(['created_at < ?', limit])
27+
logger.info("Removed '#{deleted}' expired cache entries among all repositories")
28+
end
29+
30+
31+
def clear_cache_for_repository(repo_id)
32+
deleted = GitCache.delete_all(['repo_identifier = ?', repo_id])
33+
logger.info("Removed '#{deleted}' expired cache entries for repository '#{repo_id}'")
34+
end
35+
36+
37+
def apply_cache_limit(max_cache_elements)
38+
GitCache.find(:last, order: 'created_at DESC').destroy if max_cache_elements >= 0 && GitCache.count > max_cache_elements
39+
end
40+
41+
end
42+
end

Diff for: lib/redmine_git_hosting/shell_redirector.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def logger
3535
# *options[:write_stdin]* will derive caching key from data that block writes to io stream.
3636
#
3737
def execute(cmd_str, repo_id, options = {}, &block)
38-
if !options[:write_stdin] && out = RedmineGitHosting::Cache.check_cache(cmd_str)
38+
if !options[:write_stdin] && out = RedmineGitHosting::Cache.get_cache(cmd_str)
3939
# Simple case -- have cached result that depends only on cmd_str
4040
block.call(out)
4141
retio = out
@@ -153,7 +153,7 @@ def binmode
153153

154154
def close_write
155155
# Ok -- now have all the extra args... Check cache
156-
out = RedmineGitHosting::Cache.check_cache(@my_cmd_str, @my_extra_args)
156+
out = RedmineGitHosting::Cache.get_cache(@my_cmd_str, @my_extra_args)
157157
if out
158158
# Match in the cache!
159159
@state = STRING_IO

0 commit comments

Comments
 (0)