Module: JsonSchematize::Cache::ClassMethods::Client

Defined in:
lib/json_schematize/cache/class_methods/client.rb

Instance Method Summary collapse

Instance Method Details

#__delete_record__!(key) ⇒ Object



40
41
42
# File 'lib/json_schematize/cache/class_methods/client.rb', line 40

def __delete_record__!(key)
  __update_record_keeper__!(expire: nil, cache_key: key, delete_key: true)
end

#__deserialize_keys__(key_hash_bytes) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/json_schematize/cache/class_methods/client.rb', line 5

def __deserialize_keys__(key_hash_bytes)
  return [{}, 0] if key_hash_bytes.nil?

  min_expire = Time.now.to_i
  key_hash =  __marshalize__(key_hash_bytes)
  keys_removed = 0
  key_hash.each do |key, expire|
    next if expire.to_i > min_expire

    key_hash.delete(key)
    keys_removed += 1
  end

  [key_hash, keys_removed]
rescue StandardError => e
  ::Kernel.warn("Yikes!! Failed to parse. Returning empty. #{e.message}")
  [{}, 0]
end

#__marshalize__(key_hash_bytes) ⇒ Object



24
25
26
# File 'lib/json_schematize/cache/class_methods/client.rb', line 24

def __marshalize__(key_hash_bytes)
  Marshal.load(key_hash_bytes)
end

#__update_record_keeper__!(expire:, cache_key:, delete_key: false) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/json_schematize/cache/class_methods/client.rb', line 28

def __update_record_keeper__!(expire:, cache_key:, delete_key: false)
  record_of_truth = cached_keys(with_expire: true)
  if delete_key
    record_of_truth.delete(cache_key)
  else
    record_of_truth[cache_key] = expire
  end
  serialized_string = Marshal.dump(record_of_truth)

  cache_client.write(cache_namespace, serialized_string)
end

#cached_items(key_includes: nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/json_schematize/cache/class_methods/client.rb', line 53

def cached_items(key_includes: nil)
  clear_unscored_items! if rand > cache_configuration[:stochastic_cache_bust]

  cached_keys.map do |key|
    if key_includes
      next unless key.include?(key_includes)
    end

    serialized_string = cache_client.read(key)
    Marshal.load(serialized_string)
  end.compact
end

#cached_keys(with_expire: false, count_removed: false) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/json_schematize/cache/class_methods/client.rb', line 44

def cached_keys(with_expire: false, count_removed: false)
  raw_string = cache_client.read(cache_namespace)
  key_hash, keys_removed = __deserialize_keys__(raw_string)
  return key_hash if with_expire
  return keys_removed if count_removed

  key_hash.keys
end

#clear_cache!Object



66
67
68
# File 'lib/json_schematize/cache/class_methods/client.rb', line 66

def clear_cache!
  cache_client.delete_multi(([cache_namespace] + cached_keys))
end

#clear_unscored_items!Object



70
71
72
# File 'lib/json_schematize/cache/class_methods/client.rb', line 70

def clear_unscored_items!
  cached_keys(count_removed: true)
end