| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- #include <GroupStateStore.h>
- GroupStateStore::GroupStateStore(const size_t maxSize)
- : cache(GroupStateCache(maxSize))
- { }
- GroupState& GroupStateStore::get(const GroupId& id) {
- GroupState* state = cache.get(id);
- if (state == NULL) {
- trackEviction();
- GroupState loadedState = GroupState::defaultState(id.deviceType);
- persistence.get(id, loadedState);
- state = cache.set(id, loadedState);
- }
- return *state;
- }
- GroupState& GroupStateStore::set(const GroupId &id, const GroupState& state) {
- GroupState& storedState = get(id);
- storedState = state;
- return storedState;
- }
- void GroupStateStore::trackEviction() {
- if (cache.isFull()) {
- evictedIds.add(cache.getLru());
- }
- }
- void GroupStateStore::flush() {
- ListNode<GroupCacheNode*>* curr = cache.getHead();
- while (curr != NULL && curr->data->state.isDirty()) {
- persistence.set(curr->data->id, curr->data->state);
- curr->data->state.clearDirty();
- curr = curr->next;
- }
- while (evictedIds.size() > 0) {
- persistence.clear(evictedIds.shift());
- }
- }
|