Class: TinyStruct::MemoryCache

Inherits:
Object
  • Object
show all
Defined in:
lib/tiny_struct/memory_cache.rb

Overview

Maintains an in-memory cache of the defined TinyStruct classes, such that classes with the same attribute list do not end up getting created twice.

This cache makes performance quite a bit better since looping through ObjectSpace takes a while, but takes a hit on memory because everything is now stored. Also prevents the classes from being freed if they were created anonymously and could have otherwise have been freed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMemoryCache



12
13
14
# File 'lib/tiny_struct/memory_cache.rb', line 12

def initialize
  @cache = {}
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



10
11
12
# File 'lib/tiny_struct/memory_cache.rb', line 10

def cache
  @cache
end

Instance Method Details

#[](members) ⇒ Object



20
21
22
# File 'lib/tiny_struct/memory_cache.rb', line 20

def [](members)
  cache[members]
end

#[]=(members, clazz) ⇒ Object



16
17
18
# File 'lib/tiny_struct/memory_cache.rb', line 16

def []=(members, clazz)
  cache[members] = clazz
end