From e8ccc580f4c7bd873b10452a8b4fdbf4f64d49e4 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Tue, 23 Oct 2018 06:44:42 +0200 Subject: [PATCH] reintroduce cached doi_count --- app/models/concerns/cacheable.rb | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/app/models/concerns/cacheable.rb b/app/models/concerns/cacheable.rb index bb1ac666c..e55ed0a28 100644 --- a/app/models/concerns/cacheable.rb +++ b/app/models/concerns/cacheable.rb @@ -2,6 +2,23 @@ module Cacheable extend ActiveSupport::Concern included do + def cached_doi_count(options={}) + Rails.cache.fetch("cached_doi_count/#{id}", expires_in: 24.hours, force: options[:force]) do + return [] if Rails.env.test? + + if self.class.name == "Provider" && symbol != "ADMIN" + collection = Doi.joins(:client).where("datacentre.allocator = ?", id) + elsif self.class.name == "Client" + collection = Doi.where(datacentre: id) + else + collection = Doi + end + + years = collection.order("YEAR(dataset.created)").group("YEAR(dataset.created)").count + years = years.map { |k,v| { id: k, title: k, count: v } } + end + end + def cached_metadata_count(options={}) Rails.cache.fetch("cached_metadata_count/#{id}", expires_in: 6.hours, force: options[:force]) do return [] if Rails.env.test? @@ -193,12 +210,5 @@ def cached_client_response(id, options={}) Client.where(symbol: id).first end end - - def cached_clients_by_provider_response(id, options={}) - Rails.cache.fetch("client_by_provider_response/#{id}", expires_in: 1.day) do - query = self.ds.where(is_active: true, allocator: id) - query.limit(25).offset(0).order(:name) - end - end end end