Class: MiniSql::Mysql::DeserializerCache

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_sql/mysql/deserializer_cache.rb

Constant Summary collapse

DEFAULT_MAX_SIZE =
500

Instance Method Summary collapse

Constructor Details

#initialize(max_size = nil) ⇒ DeserializerCache

Returns a new instance of DeserializerCache.



9
10
11
12
# File 'lib/mini_sql/mysql/deserializer_cache.rb', line 9

def initialize(max_size = nil)
  @cache = {}
  @max_size = max_size || DEFAULT_MAX_SIZE
end

Instance Method Details

#materialize(result, decorator_module = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mini_sql/mysql/deserializer_cache.rb', line 14

def materialize(result, decorator_module = nil)
  key = result.fields.join(',')

  # trivial fast LRU implementation
  materializer = @cache.delete(key)
  if materializer
    @cache[key] = materializer
  else
    materializer = @cache[key] = new_row_materializer(result)
    @cache.shift if @cache.length > @max_size
  end

  if decorator_module
    materializer = materializer.decorated(decorator_module)
  end

  result.map do |data|
    materializer.materialize(data)
  end
end