GroupStateStore.cpp 993 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include <GroupStateStore.h>
  2. GroupStateStore::GroupStateStore(const size_t maxSize)
  3. : cache(GroupStateCache(maxSize))
  4. { }
  5. GroupState* GroupStateStore::get(const GroupId& id) {
  6. GroupState* state = cache.get(id);
  7. if (state == NULL) {
  8. trackEviction();
  9. GroupState loadedState = GroupState::defaultState(id.deviceType);
  10. persistence.get(id, loadedState);
  11. state = cache.set(id, loadedState);
  12. }
  13. return state;
  14. }
  15. GroupState* GroupStateStore::set(const GroupId &id, const GroupState& state) {
  16. *(get(id)) = state;
  17. }
  18. void GroupStateStore::trackEviction() {
  19. if (cache.isFull()) {
  20. evictedIds.add(cache.getLru());
  21. }
  22. }
  23. void GroupStateStore::flush() {
  24. ListNode<GroupCacheNode*>* curr = cache.getHead();
  25. while (curr != NULL && curr->data->state.isDirty()) {
  26. persistence.set(curr->data->id, curr->data->state);
  27. curr->data->state.clearDirty();
  28. curr = curr->next;
  29. }
  30. while (evictedIds.size() > 0) {
  31. persistence.clear(evictedIds.shift());
  32. }
  33. }