Code Monkey home page Code Monkey logo

weak_cache's Introduction

Weak Cache

Weak cache is a Map implementation that uses WeakReferences for holding values and Finalizer to manage it's storage.

You can use it to cache data for a small amount of time until next garbage collection cycle.

Note: Values cannot be numbers, strings, booleans, records, null, dart:ffi pointers, dart:ffi structs, or dart:ffi unions.

Features

  • Uses WeakReference for storing values.
  • Uses Finalizer to remove objects from internal storage upon their deletion.
  • Allows you iterate over keys/values.

    While iterating, all stored values are temporarily made into strong references, to prevent concurrent edit of storage, while iterating over it.

  • Optimized containsValue via internal managed Expando.
  • Implements full Map<K, V> interface.
  • WeakCache itself can be safely garbage collected and doesn't hold unto any stored.

Usage

Create cache, add values, and they'll be removed when all other strong references to them are lost.

// ID - Object cache
final cache = WeakCache<int, Object>();

Object? obj = Object();
cache[0] = obj;
// ...
obj = null;
// ...
// After garbage collection cache[0] will be removed.
cache[0] == null;

See example for detailed test case.

weak_cache's People

Contributors

zekfad avatar

Stargazers

 avatar

Watchers

 avatar

weak_cache's Issues

this doesnt build on dart 3.10.6

% flutter --version
Flutter 3.10.6 • channel stable • https://github.com/flutter/flutter.git
Framework • revision f468f3366c (5 days ago) • 2023-07-12 15:19:05 -0700
Engine • revision cdbeda788a
Tools • Dart 3.0.6 • DevTools 2.23.1
% flutter run
Launching lib/main.dart on Pixel 6 Pro in debug mode...
Unhandled exception:
Invalid argument(s): Type parameter TypeParameter(CacheStrongSnapshot.K) is not indexed
#0      TypeParameterIndexer.[] (package:kernel/binary/ast_to_binary.dart:3422:8)
#1      BinaryPrinter.visitTypeParameterType (package:kernel/binary/ast_to_binary.dart:2573:38)
#2      TypeParameterType.accept (package:kernel/ast.dart:13247:42)
#3      BinaryPrinter.writeNode (package:kernel/binary/ast_to_binary.dart:446:10)
#4      BinaryPrinter.writeNodeList (package:kernel/binary/ast_to_binary.dart:347:7)
#5      BinaryPrinter.visitRecordType (package:kernel/binary/ast_to_binary.dart:2557:5)
#6      RecordType.accept (package:kernel/ast.dart:13400:14)
#7      BinaryPrinter.writeNode (package:kernel/binary/ast_to_binary.dart:446:10)
#8      BinaryPrinter.writeNodeList (package:kernel/binary/ast_to_binary.dart:347:7)
#9      BinaryPrinter.visitSupertype (package:kernel/binary/ast_to_binary.dart:2525:7)
#10     Supertype.accept (package:kernel/ast.dart:13752:34)
#11     BinaryPrinter.writeNode (package:kernel/binary/ast_to_binary.dart:446:10)
#12     BinaryPrinter.writeNodeList (package:kernel/binary/ast_to_binary.dart:347:7)
#13     BinaryPrinter.visitClass (package:kernel/binary/ast_to_binary.dart:1286:5)
#14     Class.accept (package:kernel/ast.dart:1481:38)
#15     BinaryPrinter.writeClassNode (package:kernel/binary/ast_to_binary.dart:488:10)
#16     BinaryPrinter.writeClassNodeList (package:kernel/binary/ast_to_binary.dart:374:7)
#17     BinaryPrinter.visitLibrary (package:kernel/binary/ast_to_binary.dart:1126:5)
#18     Library.accept (package:kernel/ast.dart:587:38)
#19     BinaryPrinter.writeLibraryNode (package:kernel/binary/ast_to_binary.dart:467:10)
#20     BinaryPrinter.writeLibraries (package:kernel/binary/ast_to_binary.dart:780:9)
#21     BinaryPrinter.writeComponentFile.<anonymous closure> (package:kernel/binary/ast_to_binary.dart:619:7)
#22     Timeline.timeSync (dart:developer/timeline.dart:166:22)
#23     BinaryPrinter.writeComponentFile (package:kernel/binary/ast_to_binary.dart:600:14)
#24     IncrementalSerializer.serialize (package:front_end/src/fasta/incremental_serializer.dart:308:13)
#25     IncrementalSerializer.writePackagesToSinkAndTrimComponent (package:front_end/src/fasta/incremental_serializer.dart:158:24)
#26     FrontendCompiler.writeDillFile (package:frontend_server/frontend_server.dart:827:29)
#27     FrontendCompiler.compile (package:frontend_server/frontend_server.dart:645:13)
<asynchronous suspension>
#28     listenAndCompile.<anonymous closure> (package:frontend_server/frontend_server.dart:1308:11)
<asynchronous suspension>
the Dart compiler exited unexpectedly.
the Dart compiler exited unexpectedly.
Running Gradle task 'assembleDebug'...                                 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.