From 7afc399724fa0599f2970afba94d5c34e68b13fd Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Mon, 21 Jul 2025 22:27:20 +0200 Subject: [PATCH] Add cache to points limit exceeded check --- .app_version | 2 +- CHANGELOG.md | 8 +++++++- app/services/points_limit_exceeded.rb | 9 +++++++-- spec/services/points_limit_exceeded_spec.rb | 5 +++++ 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/.app_version b/.app_version index c25c8e5b..1a44cad7 100644 --- a/.app_version +++ b/.app_version @@ -1 +1 @@ -0.30.0 +0.30.1 diff --git a/CHANGELOG.md b/CHANGELOG.md index 89e12393..04d1a143 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,10 +4,16 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +# [0.30.1] - 2025-07-21 + +## Fixed + +- Points limit exceeded check is now cached. + # [0.30.0] - 2025-07-21 -⚠️ If you were using RC, please run the following commands in the console, otherwise read on. ⚠️ +⚠️ If you were using 0.29.2 RC, please run the following commands in the console, otherwise read on. ⚠️ ```ruby # This will delete all tracks 👇 diff --git a/app/services/points_limit_exceeded.rb b/app/services/points_limit_exceeded.rb index f47543d1..2bf8de8a 100644 --- a/app/services/points_limit_exceeded.rb +++ b/app/services/points_limit_exceeded.rb @@ -7,13 +7,18 @@ class PointsLimitExceeded def call return false if DawarichSettings.self_hosted? - return true if @user.tracked_points.count >= points_limit - false + Rails.cache.fetch(cache_key, expires_in: 1.day) do + @user.tracked_points.count >= points_limit + end end private + def cache_key + "points_limit_exceeded/#{@user.id}" + end + def points_limit DawarichSettings::BASIC_PAID_PLAN_LIMIT end diff --git a/spec/services/points_limit_exceeded_spec.rb b/spec/services/points_limit_exceeded_spec.rb index 88cd6268..fed8a880 100644 --- a/spec/services/points_limit_exceeded_spec.rb +++ b/spec/services/points_limit_exceeded_spec.rb @@ -28,6 +28,11 @@ RSpec.describe PointsLimitExceeded do end it { is_expected.to be true } + + it 'caches the result' do + expect(user.tracked_points).to receive(:count).once + 2.times { described_class.new(user).call } + end end context 'when user points count exceeds the limit' do