Module: SemanticCache::Cacheable
- Defined in:
- lib/semantic_cache/rails.rb
Overview
Rails integration for SemanticCache.
In your Gemfile:
gem "semantic-cache"
Then require in an initializer:
require "semantic_cache/rails"
Usage in controllers:
class ChatController < ApplicationController
include SemanticCache::Cacheable
cache_ai_calls only: [:create], ttl: 1.hour
def create
response = SemanticCache.current.fetch(params[:message]) do
openai_client.chat(messages: [{ role: "user", content: params[:message] }])
end
render json: { response: response }
end
end
Or with per-user namespacing:
class ApplicationController < ActionController::Base
around_action :with_semantic_cache
private
def with_semantic_cache
SemanticCache.with_cache(namespace: "user_#{current_user.id}") do
yield
end
end
end
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Class Method Details
.included(base) ⇒ Object
44 45 46 |
# File 'lib/semantic_cache/rails.rb', line 44 def self.included(base) base.extend(ClassMethods) end |