Class: SemanticCache::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/semantic_cache/entry.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query:, embedding:, response:, model: nil, tags: [], ttl: nil, metadata: {}) ⇒ Entry

Returns a new instance of Entry.



9
10
11
12
13
14
15
16
17
18
# File 'lib/semantic_cache/entry.rb', line 9

def initialize(query:, embedding:, response:, model: nil, tags: [], ttl: nil, metadata: {})
  @query = query
  @embedding = embedding
  @response = response
  @model = model
  @tags = Array(tags)
  @created_at = Time.now.to_f
  @ttl = ttl
   = 
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



7
8
9
# File 'lib/semantic_cache/entry.rb', line 7

def created_at
  @created_at
end

#embeddingObject (readonly)

Returns the value of attribute embedding.



7
8
9
# File 'lib/semantic_cache/entry.rb', line 7

def embedding
  @embedding
end

#metadataObject (readonly)

Returns the value of attribute metadata.



7
8
9
# File 'lib/semantic_cache/entry.rb', line 7

def 
  
end

#modelObject (readonly)

Returns the value of attribute model.



7
8
9
# File 'lib/semantic_cache/entry.rb', line 7

def model
  @model
end

#queryObject (readonly)

Returns the value of attribute query.



7
8
9
# File 'lib/semantic_cache/entry.rb', line 7

def query
  @query
end

#responseObject (readonly)

Returns the value of attribute response.



7
8
9
# File 'lib/semantic_cache/entry.rb', line 7

def response
  @response
end

#tagsObject (readonly)

Returns the value of attribute tags.



7
8
9
# File 'lib/semantic_cache/entry.rb', line 7

def tags
  @tags
end

#ttlObject (readonly)

Returns the value of attribute ttl.



7
8
9
# File 'lib/semantic_cache/entry.rb', line 7

def ttl
  @ttl
end

Class Method Details

.from_h(hash) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/semantic_cache/entry.rb', line 43

def self.from_h(hash)
  hash = hash.transform_keys(&:to_sym) if hash.is_a?(Hash)
  entry = new(
    query: hash[:query],
    embedding: hash[:embedding],
    response: hash[:response],
    model: hash[:model],
    tags: hash[:tags] || [],
    ttl: hash[:ttl],
    metadata: hash[:metadata] || {}
  )
  entry.instance_variable_set(:@created_at, hash[:created_at]) if hash[:created_at]
  entry
end

.from_json(json_string) ⇒ Object



58
59
60
# File 'lib/semantic_cache/entry.rb', line 58

def self.from_json(json_string)
  from_h(JSON.parse(json_string))
end

Instance Method Details

#expired?Boolean

Returns:

  • (Boolean)


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

def expired?
  return false if @ttl.nil?

  Time.now.to_f - @created_at > @ttl
end

#to_hObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/semantic_cache/entry.rb', line 26

def to_h
  {
    query: @query,
    embedding: @embedding,
    response: @response,
    model: @model,
    tags: @tags,
    created_at: @created_at,
    ttl: @ttl,
    metadata: 
  }
end

#to_json(*args) ⇒ Object



39
40
41
# File 'lib/semantic_cache/entry.rb', line 39

def to_json(*args)
  to_h.to_json(*args)
end