Skip to content

Commit eef1dec

Browse files
committed
add doc comments to cache.rb
1 parent ce0fa68 commit eef1dec

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

lib/workos/cache.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
# frozen_string_literal: true
2+
13
module WorkOS
4+
# The Cache module provides a simple in-memory cache for storing values
5+
# This module is not meant to be instantiated in a user space, and is used internally by the SDK
26
module Cache
7+
# The Entry class represents a cache entry with a value and an expiration time
38
class Entry
49
attr_reader :value, :expires_at
510

@@ -8,6 +13,8 @@ def initialize(value, expires_in)
813
@expires_at = expires_in ? Time.now + expires_in : nil
914
end
1015

16+
# Checks if the entry has expired
17+
# @return [Boolean] True if the entry has expired, false otherwise
1118
def expired?
1219
return false if expires_at.nil?
1320

@@ -16,6 +23,12 @@ def expired?
1623
end
1724

1825
class << self
26+
# Fetches a value from the cache, or calls the block to fetch the value if it is not present
27+
# @param key [String] The key to fetch the value for
28+
# @param expires_in [Integer] The expiration time for the value in seconds
29+
# @param force [Boolean] If true, the value will be fetched from the block even if it is present in the cache
30+
# @param block [Proc] The block to call to fetch the value if it is not present in the cache
31+
# @return [Object] The value fetched from the cache or the block
1932
def fetch(key, expires_in: nil, force: false, &block)
2033
entry = store[key]
2134

@@ -28,33 +41,48 @@ def fetch(key, expires_in: nil, force: false, &block)
2841
entry.value
2942
end
3043

44+
# Reads a value from the cache
45+
# @param key [String] The key to read the value for
46+
# @return [Object] The value read from the cache, or nil if the value is not present or has expired
3147
def read(key)
3248
entry = store[key]
3349
return nil if entry.nil? || entry.expired?
3450

3551
entry.value
3652
end
3753

54+
# Writes a value to the cache
55+
# @param key [String] The key to write the value for
56+
# @param value [Object] The value to write to the cache
57+
# @param expires_in [Integer] The expiration time for the value in seconds
58+
# @return [Object] The value written to the cache
3859
def write(key, value, expires_in: nil)
3960
store[key] = Entry.new(value, expires_in)
4061
value
4162
end
4263

64+
# Deletes a value from the cache
65+
# @param key [String] The key to delete the value for
4366
def delete(key)
4467
store.delete(key)
4568
end
4669

70+
# Clears all values from the cache
4771
def clear
4872
store.clear
4973
end
5074

75+
# Checks if a value exists in the cache
76+
# @param key [String] The key to check for
77+
# @return [Boolean] True if the value exists and has not expired, false otherwise
5178
def exist?(key)
5279
entry = store[key]
5380
!(entry.nil? || entry.expired?)
5481
end
5582

5683
private
5784

85+
# The in-memory store for the cache
5886
def store
5987
@store ||= {}
6088
end

spec/lib/workos/cache_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
describe WorkOS::Cache do
24
before { described_class.clear }
35

0 commit comments

Comments
 (0)