Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

73 lines
2.2 KiB
Ruby
Raw Permalink Normal View History

2026-05-19 14:50:36 +02:00
# frozen_string_literal: true
2017-04-13 12:54:54 +02:00
#-- copyright
2020-01-15 11:31:26 +01:00
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
2017-04-13 12:54:54 +02:00
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
2021-01-13 17:47:45 +01:00
# Copyright (C) 2006-2013 Jean-Philippe Lang
2017-04-13 12:54:54 +02:00
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
2017-04-13 12:54:54 +02:00
#++
require_relative "cache/cache_key"
2017-04-13 12:54:54 +02:00
module OpenProject
2026-05-19 11:11:07 +02:00
##
# A cache accessor that ensures cache keys expire after OpenProject version bumps.
# Using OpenProject::Cache should be preferred over using Rails.cache directly.
2017-04-13 12:54:54 +02:00
module Cache
2026-05-19 11:05:22 +02:00
class << self
def fetch(*, **, &)
Rails.cache.fetch(CacheKey.key(*), **, &)
end
2023-11-07 12:21:14 +01:00
# Like .fetch, but caches the result in RequestStore for the
2026-05-19 19:55:48 +02:00
# lifetime of the current request.
# Useful when accessing many times during a request to avoid
# multiple cache round-trips.
def fetch_request_cached(*, **, &)
key = CacheKey.key(*)
2026-05-19 19:55:48 +02:00
RequestStore.fetch(key) do
Rails.cache.fetch(key, **, &)
end
end
2026-05-19 19:55:48 +02:00
def read(name, **)
2026-05-19 11:11:07 +02:00
Rails.cache.read(CacheKey.key(name), **)
2026-05-19 11:05:22 +02:00
end
2023-11-07 12:21:14 +01:00
2026-05-19 11:11:07 +02:00
def write(name, value, **)
Rails.cache.write(CacheKey.key(name), value, **)
end
2026-05-19 11:11:07 +02:00
def delete(*)
Rails.cache.delete(CacheKey.key(*))
2026-05-19 11:05:22 +02:00
end
2026-05-19 11:05:22 +02:00
def clear
Rails.cache.clear
end
end
2017-04-13 12:54:54 +02:00
end
end