MemoryPersistence<T> class

Abstract persistence component that stores data in memory.

This is the most basic persistence component that is only able to store data items of any type. Specific CRUD operations over the data items must be implemented in child classes by accessing this.items property and calling save method.

The component supports loading and saving items from another data source. That allows to use it as a base class for file and other types of persistence components that cache all data in memory.

References

  • *:logger:*:*:1.0 (optional) ILogger components to pass log messages

Example

class MyMemoryPersistence extends MemoryPersistence<MyData> {

   Future<MyData> getByName(String? correlationId, String name) async {
        var item = items.firstWhere((d) => d.name == name);
       return item;
    });

    Future<MyData> set(String correlatonId, MyData item) async {
        items = items.where((d) => d.name != name);
        items.add(item);
        await save(correlationId);
        return item;
    }
}

var persistence = MyMemoryPersistence();

persistence.set("123", { name: "ABC" })
var item = await persistence.getByName("123", "ABC")
print(item);                   // Result: { name: "ABC" }
Implementers

Constructors

MemoryPersistence([ILoader<T>? loader, ISaver<T>? saver])
Creates a new instance of the persistence.

Properties

hashCode int
The hash code for this object.
no setterinherited
items List<T>
getter/setter pair
loader ILoader<T>?
getter/setter pair
logger ↔ CompositeLogger
getter/setter pair
maxPageSize int
getter/setter pair
opened bool
getter/setter pair
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
saver ISaver<T>?
getter/setter pair

Methods

clear(String? correlationId) Future
Clears component state.
close(String? correlationId) Future
Closes component and frees used resources.
create(String? correlationId, T item) Future<T?>
Creates a data item.
deleteByFilterEx(String? correlationId, Function filter) Future
Deletes data items that match to a given filter.
getCountByFilterEx(String? correlationId, Function? filter) Future<int>
Gets a count of data items retrieved by a given filter.
getListByFilterEx(String? correlationId, Function? filter, Function? sort, dynamic select) Future<List<T>>
Gets a list of data items retrieved by a given filter and sorted according to sort parameters.
getOneRandom(String? correlationId, Function? filter) Future<T?>
Gets a random item from items that match to a given filter.
getPageByFilterEx(String? correlationId, Function? filter, PagingParams? paging, Function? sort, [dynamic select]) Future<DataPage<T>>
Gets a page of data items retrieved by a given filter and sorted according to sort parameters.
isOpen() bool
Checks if the component is opened.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
open(String? correlationId) Future
Opens the component.
save(String? correlationId) Future
Saves items to external data source using configured saver component.
setReferences(IReferences references) → void
Sets references to dependent components.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited