feat: use JSON as an interface to interact with cache

This commit is contained in:
Shivam Mishra
2024-08-22 17:54:56 +05:30
parent dc2c88ea8e
commit 311a0d1bc3
2 changed files with 10 additions and 2 deletions
+9 -1
View File
@@ -42,12 +42,20 @@ class Integrations::OpenaiBaseService
return nil unless event_is_cacheable?
return nil if cache_key.blank?
Redis::Alfred.get(cache_key)
value_from_cache = Redis::Alfred.get(cache_key)
return nil if value_from_cache.blank?
JSON.parse(value_from_cache, symbolize_names: true)
rescue JSON::ParserError
nil
end
def save_to_cache(response)
return nil unless event_is_cacheable?
# if the response is a hash, save it as a json
# This allows the fetching to be simplified
response = response.to_json if response.is_a?(Hash)
Redis::Alfred.setex(cache_key, response)
end
+1 -1
View File
@@ -33,7 +33,7 @@ module Redis::RedisKeys
LATEST_CHATWOOT_VERSION = 'LATEST_CHATWOOT_VERSION'.freeze
# Check if a message create with same source-id is in progress?
MESSAGE_SOURCE_KEY = 'MESSAGE_SOURCE_KEY::%<id>s'.freeze
OPENAI_CONVERSATION_KEY = 'OPEN_AI_CONVERSATION_KEY::v1::%<event_name>s::%<conversation_id>d::%<updated_at>d'.freeze
OPENAI_CONVERSATION_KEY = 'OPEN_AI_CONVERSATION_KEY::V1::%<event_name>s::%<conversation_id>d::%<updated_at>d'.freeze
## Sempahores / Locks
# We don't want to process messages from the same sender concurrently to prevent creating double conversations