Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

657 B
Raw Blame
module API
  module Caching
    module Helpers
      def with_etag!(key)
        etag = %(W/"#{::Digest::SHA1.hexdigest(key.to_s)}")
        error!("Not Modified".freeze, 304) if headers["If-None-Match".freeze] == etag

        header "ETag".freeze, etag
      end

      ##
      # Store a represented object in its JSON representation
      def cache(key, args = {})
        # Save serialization since we're only dealing with strings here
        args[:raw] = true

        json = Rails.cache.fetch(key, args) do
          result = yield
          result.to_json
        end

        ::API::Caching::StoredRepresenter.new json
      end
    end
  end
end