grape-listing 1.1.4 → 1.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 45e458a8f8256d37914e7844b87ab5ca89a828649e7de41743495dafa2122bbd
4
- data.tar.gz: 6fc8dd5d2f6ae95c8122c74f895129282835bf42c45d8fd8f553d6df69102f16
3
+ metadata.gz: ebde4f181db7c89e1def2eeb1af0fe820da5f89b0921dc88080d0b793f3d01ac
4
+ data.tar.gz: 5f077bb264dcf16c617be8a22c78e0769a1270af387ee2f171b78be398a35baa
5
5
  SHA512:
6
- metadata.gz: 87a26725a413209783864fb440ea285aaae589b80597f4ae23217dd135e1c380fd3ba8d6e3e74640bb2890d4a947ac1c41a6c75ba2560c862c3f56cf4ff0f44d
7
- data.tar.gz: 470b431dbb7f26af609bfe09de5ad38ddb3cf7d004880c276ed8c77acade62107a88f57dc6a936b0a385fa349f5908be55a58b8e6c62f241f61e612dde1e1641
6
+ metadata.gz: 0cc783b3c30a87083033d825a7d80624af8d513c55989b6cd572a9dea1786caa72bc1306f50bf1a5f6ca6f136e9008b4c08a6b93713d4e5da58b12e06684500d
7
+ data.tar.gz: dde882747da769a523f0c31b4862327d8b959f667a14e5b151b92dca8de9b609204467c2b9bfbfc79e19b75a2b27b49243184ac434bb7ee858a496371b6c6c78
data/lib/grape/dsl.rb CHANGED
@@ -3,7 +3,12 @@ module Grape
3
3
  module InsideRoute
4
4
 
5
5
  def listing(model:, entity:, scopes: nil, search: nil, paginate: true, caching: false)
6
- opts = listing_opts(model, entity, scopes, search, caching)
6
+ # параметры запроса API
7
+ request_method = request.env['REQUEST_METHOD']
8
+ request_uri = request.env['REQUEST_URI']
9
+
10
+ # опции для сервиса
11
+ opts = listing_opts(model, entity, scopes, search, caching, request_method, request_uri)
7
12
 
8
13
  if params[:spreadsheet]
9
14
  listing_spreadsheet(**opts)
@@ -16,9 +21,9 @@ module Grape
16
21
 
17
22
  private
18
23
 
19
- def listing_opts(model, entity, scopes, search, caching)
24
+ def listing_opts(model, entity, scopes, search, caching, request_method, request_uri)
20
25
  # стандартные опции
21
- opts = { model:, entity:, scopes:, search:, params:, current_user:, caching: }
26
+ opts = { model:, entity:, scopes:, search:, params:, current_user:, caching:, request_method:, request_uri: }
22
27
 
23
28
  # требуемый список полей (+стандартные) для фильтрации в Grape Entity
24
29
  if params[:columns]
@@ -7,6 +7,10 @@ module GrapeListing
7
7
  # обрабатываемая модель
8
8
  @model = args[:model]
9
9
 
10
+ # параметры адреса запроса API
11
+ @request_method = args[:request_method]
12
+ @request_uri = args[:request_uri]
13
+
10
14
  # параметры запроса и текущий пользователь
11
15
  @params = args[:params].stringify_keys
12
16
  @current_user = args[:current_user]
@@ -49,8 +53,8 @@ module GrapeListing
49
53
 
50
54
  def records_count
51
55
  # получение кол-ва записей из кеша
52
- cache_key = "#{@model}_records_count"
53
- cached = GrapeListing.cache.read(cache_key)
56
+ key = count_cache_key
57
+ cached = GrapeListing.cache.read(key)
54
58
  return cached if cached
55
59
 
56
60
  # получение кол-ва записей из запроса к БД
@@ -58,11 +62,19 @@ module GrapeListing
58
62
 
59
63
  # кеширование кол-ва записей при превышении порогового значения
60
64
  if count >= 1_000
61
- GrapeListing.cache.write(cache_key, count, expires_in: 30.minutes)
65
+ GrapeListing.cache.write(key, count, expires_in: 30.minutes)
62
66
  end
63
67
 
64
68
  count
65
69
  end
66
70
 
71
+ def count_cache_key
72
+ # пример: "GET api/v1/users?active=true"
73
+ body = "#{@request_method} #{@request_uri}"
74
+
75
+ enc = Digest::MD5.hexdigest(body)
76
+ "listing_cached_count_#{enc}"
77
+ end
78
+
67
79
  end
68
80
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape-listing
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.4
4
+ version: 1.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Павел Бабин
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-05-15 00:00:00.000000000 Z
11
+ date: 2024-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport