Module: SemanticCache

Defined in:
lib/semantic_cache.rb,
lib/semantic_cache/cache.rb,
lib/semantic_cache/entry.rb,
lib/semantic_cache/rails.rb,
lib/semantic_cache/stats.rb,
lib/semantic_cache/version.rb,
lib/semantic_cache/embedding.rb,
lib/semantic_cache/similarity.rb,
lib/semantic_cache/stores/redis.rb,
lib/semantic_cache/configuration.rb,
lib/semantic_cache/stores/memory.rb,
lib/semantic_cache/client_wrapper.rb

Defined Under Namespace

Modules: Cacheable, Similarity, Stores Classes: Cache, ClientWrapper, Configuration, ConfigurationError, Embedding, Entry, Error, Stats, StoreError

Constant Summary collapse

VERSION =
"0.2.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject



22
23
24
# File 'lib/semantic_cache.rb', line 22

def configuration
  @configuration ||= Configuration.new
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Yields:



26
27
28
# File 'lib/semantic_cache.rb', line 26

def configure
  yield(configuration)
end

.currentObject

Thread-local cache instance for use within a request.



63
64
65
# File 'lib/semantic_cache/rails.rb', line 63

def current
  Thread.current[:semantic_cache_instance]
end

.new(**options) ⇒ Object

Convenience method to create a new cache instance



35
36
37
# File 'lib/semantic_cache.rb', line 35

def new(**options)
  Cache.new(**options)
end

.reset!Object



30
31
32
# File 'lib/semantic_cache.rb', line 30

def reset!
  @configuration = Configuration.new
end

.with_cache(namespace: nil, **options) ⇒ Object

Set a cache instance for the current thread/request scope.



68
69
70
71
72
73
74
# File 'lib/semantic_cache/rails.rb', line 68

def with_cache(namespace: nil, **options)
  previous = Thread.current[:semantic_cache_instance]
  Thread.current[:semantic_cache_instance] = Cache.new(namespace: namespace, **options)
  yield
ensure
  Thread.current[:semantic_cache_instance] = previous
end

.wrap(client, **options) ⇒ Object

Convenience: wrap a client with semantic caching.

cached_client = SemanticCache.wrap(openai_client)


62
63
64
# File 'lib/semantic_cache/client_wrapper.rb', line 62

def wrap(client, **options)
  ClientWrapper.new(client, **options)
end