Class: BetterTranslate::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/better_translate/cache.rb

Overview

LRU (Least Recently Used) Cache implementation

Thread-safe cache with configurable capacity and optional TTL.

Examples:

Basic usage

cache = Cache.new(capacity: 100)
cache.set("hello:it", "ciao")
cache.get("hello:it") #=> "ciao"

With TTL

cache = Cache.new(capacity: 100, ttl: 3600)
cache.set("key", "value")
# After 3600 seconds, get("key") will return nil

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(capacity: 1000, ttl: nil) ⇒ Cache

Initialize a new cache

Examples:

Create cache with capacity and TTL

cache = Cache.new(capacity: 500, ttl: 1800)

Parameters:

  • capacity (Integer) (defaults to: 1000)

    Maximum cache size

  • ttl (Integer, nil) (defaults to: nil)

    Time to live in seconds (nil = no expiration)



33
34
35
36
37
38
# File 'lib/better_translate/cache.rb', line 33

def initialize(capacity: 1000, ttl: nil)
  @capacity = capacity
  @ttl = ttl
  @cache = {}
  @mutex = Mutex.new
end

Instance Attribute Details

#capacityInteger (readonly)

Returns Maximum number of items in cache.

Returns:

  • (Integer)

    Maximum number of items in cache



20
21
22
# File 'lib/better_translate/cache.rb', line 20

def capacity
  @capacity
end

#ttlInteger? (readonly)

Returns Time to live in seconds.

Returns:

  • (Integer, nil)

    Time to live in seconds



23
24
25
# File 'lib/better_translate/cache.rb', line 23

def ttl
  @ttl
end

Instance Method Details

#clearvoid

This method returns an undefined value.

Clear the cache

Examples:

Clear all cached values

cache.clear


97
98
99
# File 'lib/better_translate/cache.rb', line 97

def clear
  @mutex.synchronize { @cache.clear }
end

#get(key) ⇒ String?

Get a value from the cache

Examples:

Get cached value

value = cache.get("translation:en:it:hello")

Parameters:

  • key (String)

    Cache key

Returns:

  • (String, nil)

    Cached value or nil if not found/expired



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/better_translate/cache.rb', line 48

def get(key)
  @mutex.synchronize do
    return nil unless @cache.key?(key)

    entry = @cache[key]

    # Check TTL
    ttl_value = @ttl
    if ttl_value && entry && Time.now - entry[:timestamp] > ttl_value
      @cache.delete(key)
      return nil
    end

    # Move to end (most recently used)
    @cache.delete(key)
    @cache[key] = entry
    entry[:value]
  end
end

#key?(key) ⇒ Boolean

Check if key exists in cache

Examples:

Check if key exists

if cache.key?("translation:en:it:hello")
  puts "Translation is cached"
end

Parameters:

  • key (String)

    Cache key

Returns:

  • (Boolean)

    true if key exists and not expired



122
123
124
# File 'lib/better_translate/cache.rb', line 122

def key?(key)
  !get(key).nil?
end

#set(key, value) ⇒ String

Set a value in the cache

Examples:

Store value in cache

cache.set("translation:en:it:hello", "ciao")

Parameters:

  • key (String)

    Cache key

  • value (String)

    Value to cache

Returns:

  • (String)

    The cached value



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/better_translate/cache.rb', line 77

def set(key, value)
  @mutex.synchronize do
    # Remove oldest entry if at capacity
    @cache.shift if @cache.size >= @capacity && !@cache.key?(key)

    @cache[key] = {
      value: value,
      timestamp: Time.now
    }
    value
  end
end

#sizeInteger

Get cache size

Examples:

Check cache size

puts "Cache contains #{cache.size} items"

Returns:

  • (Integer)

    Number of items in cache



108
109
110
# File 'lib/better_translate/cache.rb', line 108

def size
  @mutex.synchronize { @cache.size }
end