diff options
| author | Vladimir Kochnev <[email protected]> | 2016-08-03 12:09:19 +0300 |
|---|---|---|
| committer | Vladimir Kochnev <[email protected]> | 2019-10-02 18:06:35 +0300 |
| commit | 913003eaf0456e4645cad91f2622354deae10841 (patch) | |
| tree | 27293f11b37df246eaf15da6a062bbfade8882bb /lib/axlsx/package.rb | |
| parent | c73b36d80f6a07862ead30a593f62b4797926110 (diff) | |
| download | caxlsx-913003eaf0456e4645cad91f2622354deae10841.tar.gz caxlsx-913003eaf0456e4645cad91f2622354deae10841.zip | |
Fix Relationship.instances cache.
This PR aims to fix several issues with Relationship cache:
1) It's not threadsafe, so I propose to use a TLS variable for this.
2) Memory obtained by cache remains non-freed before the next run of `serialize`. I think it should be freed immediately.
3) Memory should be freed in `ensure` block to prevent memory bloating in case of exception.
*There are only two hard things in Computer Science: cache invalidation and naming things.*
Diffstat (limited to 'lib/axlsx/package.rb')
| -rw-r--r-- | lib/axlsx/package.rb | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/lib/axlsx/package.rb b/lib/axlsx/package.rb index da661b34..2e9e14fc 100644 --- a/lib/axlsx/package.rb +++ b/lib/axlsx/package.rb @@ -100,11 +100,13 @@ module Axlsx # File.open('example_streamed.xlsx', 'w') { |f| f.write(s.read) } def serialize(output, confirm_valid=false) return false unless !confirm_valid || self.validate.empty? - Relationship.clear_cached_instances + Relationship.initialize_cached_instances Zip::OutputStream.open(output) do |zip| write_parts(zip) end true + ensure + Relationship.clear_cached_instances end @@ -113,11 +115,13 @@ module Axlsx # @return [StringIO|Boolean] False if confirm_valid and validation errors exist. rewound string IO if not. def to_stream(confirm_valid=false) return false unless !confirm_valid || self.validate.empty? - Relationship.clear_cached_instances + Relationship.initialize_cached_instances zip = write_parts(Zip::OutputStream.new(StringIO.new, true)) stream = zip.close_buffer stream.rewind stream + ensure + Relationship.clear_cached_instances end # Encrypt the package into a CFB using the password provided |
