fastentry 0.1.7 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 140aa4a24b917d6ea3606101cf4a05558c2a02d7c76e53005c0cb20731ead079
4
- data.tar.gz: 1130e8c02447b5dee763ceacd84c39f0bca2e149e0a68f81973bc1b068746bf2
3
+ metadata.gz: 96ab9a433cd14cadcd4bc35ccd1073a4b716d5b3890700cd0a6408ba4617ab2e
4
+ data.tar.gz: 19e9237bbe5093511e5df18a19046586bf2718c964cd4b44ab62ea136685b2b1
5
5
  SHA512:
6
- metadata.gz: b34b9fca1a73edb59eefaceed0c8e0d2466cb133e193f249c4c65ba600c22169d824ce70fe7317b2e591dbab400b3749846449fd87185987935e14a6a3d4386d
7
- data.tar.gz: 3d27170aecafb5f42b0a2b11597c745352ab78baec018ceb7da324d4ce5f99223be209330bf026a06f799b57e9ba56f714888b36b3509756a4e87355cd0071d7
6
+ metadata.gz: 5602215e206f29371fa28c8c213123c49fc5dc1f66d02d4c8d12badfb5e45d43664ceb7f62a987bed17062a295c374feeb8f55ff0881d261eb9a8679ce39fe7d
7
+ data.tar.gz: 228749dc043221af2f98b81dabf232a71ed9bdc62ecb2f5363513988922b76ac242c6504562afbc4d9ca67a367f953d34cbad09b4fce55bb5c4b6509aa7a3616
@@ -2,20 +2,16 @@ module Fastentry
2
2
  class CacheController < Fastentry::ApplicationController
3
3
  def show
4
4
  @key = params[:key]
5
-
6
- begin
7
- expiration_date = Rails.cache.send(:read_entry, key, {}).expires_at
8
- rescue
9
- expiration_date = nil
10
- end
11
5
 
12
- @expiration = (Time.at(expiration_date).strftime("%a, %e %b %Y %H:%M:%S %z") if expiration_date.present?)
13
- @cache_item = Rails.cache.read(@key)
6
+ expiration = Fastentry.cache.expiration_for(@key)
7
+ @expiration = expiration.strftime("%a, %e %b %Y %H:%M:%S %z") if expiration
8
+
9
+ @cache_item = Fastentry.cache.read(@key)
14
10
  end
15
11
 
16
12
  def invalidate
17
13
  key = params[:key]
18
- Rails.cache.delete(key)
14
+ Fastentry.cache.delete(key)
19
15
 
20
16
  redirect_back(fallback_location: root_path)
21
17
  end
@@ -3,38 +3,23 @@ module Fastentry
3
3
  before_action :set_page, only: [:index]
4
4
 
5
5
  def index
6
- @keys = Rails.cache.instance_variable_get(:@data).keys
6
+ @keys = Fastentry.cache.keys
7
7
 
8
8
  if params[:query].present?
9
- @keys = @keys.select { |key| key.downcase.include?(params[:query].downcase) }
9
+ @keys.select! { |key| key.downcase.include?(params[:query].downcase) }
10
10
  end
11
11
 
12
12
  @number_of_pages = (@keys.count / @per_page.to_f).ceil
13
-
14
13
  @keys = @keys[@offset, @per_page] || []
15
14
 
16
- @cached = []
17
- @keys.each do |key|
18
- # Prevent from crashing if expiration can't be read (temporary fix)
19
- begin
20
- expiration_date = Rails.cache.send(:read_entry, key, {}).expires_at
21
- rescue
22
- expiration_date = nil
23
- end
24
-
25
- # Prevent from crashing if can't read key
26
- begin
27
- value = Rails.cache.read(key)
28
- rescue
29
- value = nil
15
+ @cached =
16
+ @keys.map do |key|
17
+ {
18
+ cache_key: key,
19
+ cache_value: Fastentry.cache.read(key),
20
+ expiration: Fastentry.cache.expiration_for(key)
21
+ }
30
22
  end
31
-
32
- @cached << {
33
- cache_key: key,
34
- cache_value: value,
35
- expiration: (Time.at(expiration_date) if expiration_date.present?)
36
- }
37
- end
38
23
  end
39
24
 
40
25
  private
@@ -1,7 +1,7 @@
1
1
  module Fastentry
2
2
  class StatsController < Fastentry::ApplicationController
3
3
  def index
4
- @number_of_keys = Rails.cache.instance_variable_get(:@data).keys.count
4
+ @number_of_keys = Fastentry.cache.keys.size
5
5
  end
6
6
  end
7
7
  end
@@ -1,3 +1,3 @@
1
1
  module Fastentry
2
- VERSION = '0.1.7'
2
+ VERSION = '0.1.8'
3
3
  end
data/lib/fastentry.rb CHANGED
@@ -1,6 +1,52 @@
1
- require "fastentry/engine"
1
+ require 'fastentry/engine'
2
2
  require 'awesome_print'
3
3
 
4
4
  module Fastentry
5
- # Your code goes here...
5
+ class Cache < SimpleDelegator
6
+ alias cache __getobj__
7
+
8
+ def expiration_for(key)
9
+ expires_at = cache.send(:read_entry, key, {}).expires_at
10
+ expires_at.to_s.strip.empty? ? nil : Time.at(expires_at)
11
+ rescue StandardError
12
+ nil
13
+ end
14
+
15
+ def read(key)
16
+ cache.read(key)
17
+ rescue StandardError
18
+ nil
19
+ end
20
+
21
+ def self.for(cache)
22
+ case cache
23
+ when ActiveSupport::Cache::Strategy::LocalCache
24
+ DefaultCache.new(cache)
25
+ when ActiveSupport::Cache::FileStore
26
+ DefaultCache.new(cache)
27
+ when ActiveSupport::Cache::MemoryStore
28
+ DefaultCache.new(cache)
29
+ when ActiveSupport::Cache::RedisCacheStore
30
+ RedisCache.new(cache)
31
+ else
32
+ raise ArgumentError, 'Unsupported cache type'
33
+ end
34
+ end
35
+ end
36
+
37
+ class DefaultCache < Cache
38
+ def keys
39
+ cache.instance_variable_get(:@data).keys
40
+ end
41
+ end
42
+
43
+ class RedisCache < Cache
44
+ def keys
45
+ cache.redis.keys
46
+ end
47
+ end
48
+
49
+ def self.cache
50
+ @cache ||= Cache.for(Rails.cache)
51
+ end
6
52
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fastentry
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tiago Alves
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-14 00:00:00.000000000 Z
11
+ date: 2019-02-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.3.13
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.3.13
41
55
  description: Cache management for Rails
42
56
  email:
43
57
  - alvesjtiago@gmail.com