Skip to content

Commit 6049fc4

Browse files
author
root
committed
Clarify Cache interface
1 parent 903ac01 commit 6049fc4

File tree

7 files changed

+186
-154
lines changed

7 files changed

+186
-154
lines changed

Diff for: app/models/concerns/gitolitable_cache.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def find_by_path(path, flags = {})
5959

6060
# If repo identifiers unique, identifier forms unique label
6161
# Else, use directory notation: <project identifier>/<repo identifier>
62+
#
6263
def git_cache_id
6364
if identifier.blank?
6465
# Should only happen with one repo/project (the default)
@@ -71,9 +72,10 @@ def git_cache_id
7172
end
7273

7374

74-
# Note: CacheManager doesn't know about repository object, it only know git_cache_id.
75+
# Note: RedmineGitHosting::Cache doesn't know about repository object, it only knows git_cache_id.
76+
#
7577
def empty_cache!
76-
RedmineGitHosting::CacheManager.clear_cache_for_repository(git_cache_id)
78+
RedmineGitHosting::Cache.clear_cache_for_repository(git_cache_id)
7779
end
7880

7981
end

Diff for: app/use_cases/apply_settings.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def check_hooks_install
107107

108108
def check_cache_config
109109
## Gitolite cache has changed, clear cache entries!
110-
RedmineGitHosting::CacheManager.clear_obsolete_cache_entries if value_has_changed?(:gitolite_cache_max_time)
110+
RedmineGitHosting::Cache.clear_obsolete_cache_entries if value_has_changed?(:gitolite_cache_max_time)
111111
end
112112

113113

Diff for: lib/redmine_git_hosting/cache.rb

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
module RedmineGitHosting
2+
module Cache
3+
4+
class << self
5+
6+
def max_cache_time
7+
RedmineGitHosting::Config.gitolite_cache_max_time
8+
end
9+
10+
11+
def max_cache_elements
12+
RedmineGitHosting::Config.gitolite_cache_max_elements
13+
end
14+
15+
16+
# Used in ShellRedirector but define here to keep a clean interface.
17+
def max_cache_size
18+
RedmineGitHosting::Config.gitolite_cache_max_size
19+
end
20+
21+
22+
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))
25+
command = compose_key(primary_key, secondary_key)
26+
set_cache_entry(command, out_value, repo_id)
27+
end
28+
29+
30+
def check_cache(primary_key, secondary_key = nil)
31+
cached = get_cache_entry(primary_key, secondary_key)
32+
33+
if cached
34+
if valid_cache_entry?(cached)
35+
# Update updated_at flag
36+
cached.touch unless cached.command_output.nil?
37+
out = cached.command_output
38+
else
39+
cached.destroy
40+
out = nil
41+
end
42+
else
43+
out = nil
44+
end
45+
46+
# Return result as a string stream
47+
out.nil? ? nil : StringIO.new(out)
48+
end
49+
50+
51+
# After resetting cache timing parameters -- delete entries that no-longer match
52+
def clear_obsolete_cache_entries
53+
return if max_cache_time < 0 # No expiration needed
54+
limit = Time.now - max_cache_time
55+
do_clear_obsolete_cache_entries(limit)
56+
end
57+
58+
59+
# Clear the cache entries for given repository / git_cache_id
60+
def clear_cache_for_repository(repo_id)
61+
do_clear_cache_for_repository(repo_id)
62+
end
63+
64+
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
81+
82+
83+
def get_cache_entry(primary_key, secondary_key)
84+
GitCache.find_by_command(compose_key(primary_key, secondary_key))
85+
end
86+
87+
88+
def valid_cache_entry?(cached)
89+
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)
91+
(!expired || max_cache_time < 0) ? true : false
92+
end
93+
94+
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+
100+
def compose_key(key1, key2)
101+
if key2 && !key2.blank?
102+
key1 + "\n" + key2
103+
else
104+
key1
105+
end
106+
end
107+
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+
125+
end
126+
127+
end
128+
end

Diff for: lib/redmine_git_hosting/cache_manager.rb

-133
This file was deleted.

Diff for: lib/redmine_git_hosting/patches/xitolite_adapter_patch.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def git_cmd_with_git_hosting(args, options = {}, &block)
4949

5050
if !git_cache_id.nil? && git_cache_enabled?
5151
# Insert cache between shell execution and caller
52-
RedmineGitHosting::CacheManager.execute(cmd_str, git_cache_id, options, &block)
52+
RedmineGitHosting::ShellRedirector.execute(cmd_str, git_cache_id, options, &block)
5353
else
5454
Redmine::Scm::Adapters::AbstractAdapter.shellout(cmd_str, options, &block)
5555
end

Diff for: lib/redmine_git_hosting/plugins/sweepers/repository_deletor.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def move_repository_to_recycle
2424

2525
def remove_git_cache
2626
logger.info("Clean cache for repository '#{gitolite_repo_name}'")
27-
RedmineGitHosting::CacheManager.clear_cache_for_repository(git_cache_id)
27+
RedmineGitHosting::Cache.clear_cache_for_repository(git_cache_id)
2828
end
2929

3030
end

0 commit comments

Comments
 (0)