Class: SolidCacheDashboard::Decorators::CacheEntryDecorator

Inherits:
Object
  • Object
show all
Defined in:
lib/solid_cache_dashboard/decorators/cache_entry_decorator.rb

Instance Method Summary collapse

Constructor Details

#initialize(entry) ⇒ CacheEntryDecorator

Returns a new instance of CacheEntryDecorator.



4
5
6
# File 'lib/solid_cache_dashboard/decorators/cache_entry_decorator.rb', line 4

def initialize(entry)
  @entry = entry
end

Instance Method Details

#byte_sizeObject



24
25
26
# File 'lib/solid_cache_dashboard/decorators/cache_entry_decorator.rb', line 24

def byte_size
  @entry.byte_size
end

#created_atObject



32
33
34
# File 'lib/solid_cache_dashboard/decorators/cache_entry_decorator.rb', line 32

def created_at
  @entry.created_at
end

#created_at_agoObject



36
37
38
# File 'lib/solid_cache_dashboard/decorators/cache_entry_decorator.rb', line 36

def created_at_ago
  time_ago_in_words(created_at, Time.now)
end

#encrypted?Boolean

Returns:

  • (Boolean)


66
67
68
69
# File 'lib/solid_cache_dashboard/decorators/cache_entry_decorator.rb', line 66

def encrypted?
  return false unless defined?(SolidCache.configuration)
  SolidCache.configuration.encrypt?
end

#encryption_statusObject



71
72
73
# File 'lib/solid_cache_dashboard/decorators/cache_entry_decorator.rb', line 71

def encryption_status
  encrypted? ? "Encrypted" : "Not encrypted"
end

#estimated_row_overheadObject



75
76
77
78
79
80
81
82
83
# File 'lib/solid_cache_dashboard/decorators/cache_entry_decorator.rb', line 75

def estimated_row_overhead
  if encrypted?
    SolidCache::Entry::ESTIMATED_ROW_OVERHEAD + SolidCache::Entry::ESTIMATED_ENCRYPTION_OVERHEAD
  else
    SolidCache::Entry::ESTIMATED_ROW_OVERHEAD
  end
rescue NameError
  140 # fallback to default ESTIMATED_ROW_OVERHEAD
end

#expires_atObject



51
52
53
54
# File 'lib/solid_cache_dashboard/decorators/cache_entry_decorator.rb', line 51

def expires_at
  return unless @entry.respond_to?(:expires_at)
  @entry.expires_at
end

#expires_inObject



56
57
58
59
60
61
62
63
64
# File 'lib/solid_cache_dashboard/decorators/cache_entry_decorator.rb', line 56

def expires_in
  return unless expires_at
  
  if expires_at <= Time.now
    "Expired #{time_ago_in_words(expires_at, Time.now)}"
  else
    time_ago_in_words(Time.now, expires_at)
  end
end

#human_byte_sizeObject



28
29
30
# File 'lib/solid_cache_dashboard/decorators/cache_entry_decorator.rb', line 28

def human_byte_size
  ActiveSupport::NumberHelper.number_to_human_size(byte_size)
end

#keyObject



12
13
14
15
16
# File 'lib/solid_cache_dashboard/decorators/cache_entry_decorator.rb', line 12

def key
  @entry.key
rescue
  key_hash
end

#key_hashObject



8
9
10
# File 'lib/solid_cache_dashboard/decorators/cache_entry_decorator.rb', line 8

def key_hash
  @entry.key_hash
end

#key_sizeObject



85
86
87
# File 'lib/solid_cache_dashboard/decorators/cache_entry_decorator.rb', line 85

def key_size
  @entry.key.to_s.bytesize
end

#overhead_percentageObject



97
98
99
100
# File 'lib/solid_cache_dashboard/decorators/cache_entry_decorator.rb', line 97

def overhead_percentage
  return 0 if byte_size == 0
  (overhead_size.to_f / byte_size * 100).round(1)
end

#overhead_sizeObject



93
94
95
# File 'lib/solid_cache_dashboard/decorators/cache_entry_decorator.rb', line 93

def overhead_size
  byte_size - key_size - value_size
end

#statusObject



40
41
42
# File 'lib/solid_cache_dashboard/decorators/cache_entry_decorator.rb', line 40

def status
  expires_at.present? && expires_at <= Time.now ? SolidCacheDashboard::CacheEntry::EXPIRED : SolidCacheDashboard::CacheEntry::ACTIVE
end

#status_badgeObject



44
45
46
47
48
49
# File 'lib/solid_cache_dashboard/decorators/cache_entry_decorator.rb', line 44

def status_badge
  status_text = status.to_s.capitalize
  color = SolidCacheDashboard::CacheEntry.status_color(status)
  
  "<span class='inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-#{color}-100 dark:bg-#{color}-800 text-#{color}-800 dark:text-#{color}-100'>#{status_text}</span>".html_safe
end

#time_ago_in_words(from_time, to_time = Time.now) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/solid_cache_dashboard/decorators/cache_entry_decorator.rb', line 119

def time_ago_in_words(from_time, to_time = Time.now)
  distance_in_seconds = (to_time - from_time).abs.round
  suffix = to_time > from_time ? "ago" : "from now"
  
  time_words = case distance_in_seconds
  when 0..59
    "#{distance_in_seconds} seconds"
  when 60..3599
    "#{(distance_in_seconds / 60).round} minutes"
  when 3600..86399
    "#{(distance_in_seconds / 3600).round} hours"
  else
    "#{(distance_in_seconds / 86400).round} days"
  end
  
  "#{time_words} #{suffix}"
end

#total_lifetimeObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/solid_cache_dashboard/decorators/cache_entry_decorator.rb', line 102

def total_lifetime
  return nil unless expires_at && created_at
  
  distance_in_seconds = (expires_at - created_at).round
  
  case distance_in_seconds
  when 0..59
    "#{distance_in_seconds} seconds"
  when 60..3599
    "#{(distance_in_seconds / 60).round} minutes"
  when 3600..86399
    "#{(distance_in_seconds / 3600).round} hours"
  else
    "#{(distance_in_seconds / 86400).round} days"
  end
end

#valueObject



18
19
20
21
22
# File 'lib/solid_cache_dashboard/decorators/cache_entry_decorator.rb', line 18

def value
  @value ||= @entry.value.to_s
rescue
  "Binary data"
end

#value_sizeObject



89
90
91
# File 'lib/solid_cache_dashboard/decorators/cache_entry_decorator.rb', line 89

def value_size
  @entry.value.to_s.bytesize
end