Class: AvroTurf::DiskCache
- Inherits:
-
Object
- Object
- AvroTurf::DiskCache
- Defined in:
- lib/avro_turf/disk_cache.rb
Overview
A cache for the CachedConfluentSchemaRegistry. Extends the InMemoryCache to provide a write-thru to disk for persistent cache.
Instance Method Summary collapse
-
#initialize(disk_path, logger: Logger.new($stdout)) ⇒ DiskCache
constructor
A new instance of DiskCache.
-
#lookup_by_id(id) ⇒ Object
override the write-thru cache (json) does not store keys in numeric format so, convert id to a string for caching purposes.
-
#lookup_by_schema(subject, schema) ⇒ Object
override to use a json serializable cache key.
-
#lookup_by_version(subject, version) ⇒ Object
checks instance var (in-memory cache) for schema checks disk cache if in-memory cache doesn’t exists if file exists but no in-memory cache, read from file and sync in-memory cache finally, if file doesn’t exist return nil.
-
#store_by_id(id, schema) ⇒ Object
override to include write-thru cache after storing result from upstream.
-
#store_by_schema(subject, schema, id) ⇒ Object
override to use a json serializable cache key and update the file cache.
-
#store_by_version(subject, version, schema) ⇒ Object
check if file exists and parse json into a hash if file exists take json and overwite/insert schema at key if file doesn’t exist create new hash write the new/updated hash to file update instance var (in memory-cache) to match.
Constructor Details
#initialize(disk_path, logger: Logger.new($stdout)) ⇒ DiskCache
Returns a new instance of DiskCache.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/avro_turf/disk_cache.rb', line 5 def initialize(disk_path, logger: Logger.new($stdout)) @logger = logger # load the write-thru cache on startup, if it exists @schemas_by_id_path = File.join(disk_path, 'schemas_by_id.json') hash = read_from_disk_cache(@schemas_by_id_path) @schemas_by_id = hash || {} @ids_by_schema_path = File.join(disk_path, 'ids_by_schema.json') hash = read_from_disk_cache(@ids_by_schema_path) @ids_by_schema = hash || {} @schemas_by_subject_version_path = File.join(disk_path, 'schemas_by_subject_version.json') @schemas_by_subject_version = {} end |
Instance Method Details
#lookup_by_id(id) ⇒ Object
override the write-thru cache (json) does not store keys in numeric format so, convert id to a string for caching purposes
24 25 26 |
# File 'lib/avro_turf/disk_cache.rb', line 24 def lookup_by_id(id) @schemas_by_id[id.to_s] end |
#lookup_by_schema(subject, schema) ⇒ Object
override to use a json serializable cache key
38 39 40 41 |
# File 'lib/avro_turf/disk_cache.rb', line 38 def lookup_by_schema(subject, schema) key = "#{subject}#{schema}" @ids_by_schema[key] end |
#lookup_by_version(subject, version) ⇒ Object
checks instance var (in-memory cache) for schema checks disk cache if in-memory cache doesn’t exists if file exists but no in-memory cache, read from file and sync in-memory cache finally, if file doesn’t exist return nil
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/avro_turf/disk_cache.rb', line 56 def lookup_by_version(subject, version) key = "#{subject}#{version}" schema = @schemas_by_subject_version[key] return schema unless schema.nil? hash = read_from_disk_cache(@schemas_by_subject_version_path) if hash @schemas_by_subject_version = hash @schemas_by_subject_version[key] end end |
#store_by_id(id, schema) ⇒ Object
override to include write-thru cache after storing result from upstream
29 30 31 32 33 34 35 |
# File 'lib/avro_turf/disk_cache.rb', line 29 def store_by_id(id, schema) # must return the value from storing the result (i.e. do not return result from file write) @schemas_by_id[id.to_s] = schema write_to_disk_cache(@schemas_by_id_path, @schemas_by_id) schema end |
#store_by_schema(subject, schema, id) ⇒ Object
override to use a json serializable cache key and update the file cache
44 45 46 47 48 49 50 |
# File 'lib/avro_turf/disk_cache.rb', line 44 def store_by_schema(subject, schema, id) key = "#{subject}#{schema}" @ids_by_schema[key] = id write_to_disk_cache(@ids_by_schema_path, @ids_by_schema) id end |
#store_by_version(subject, version, schema) ⇒ Object
check if file exists and parse json into a hash if file exists take json and overwite/insert schema at key if file doesn’t exist create new hash write the new/updated hash to file update instance var (in memory-cache) to match
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/avro_turf/disk_cache.rb', line 74 def store_by_version(subject, version, schema) key = "#{subject}#{version}" hash = read_from_disk_cache(@schemas_by_subject_version_path) hash = if hash hash[key] = schema hash else { key => schema } end write_to_disk_cache(@schemas_by_subject_version_path, hash) @schemas_by_subject_version = hash @schemas_by_subject_version[key] end |