| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- #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) {
- *(get(id)) = state;
- }
- 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());
- }
- }
|