|
| 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 |
0 commit comments