Jamba C++ API  4.4.0
ExpiringDataCache.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 pongasoft
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  *
16  * @author Yan Pujante
17  */
18 
19 #ifndef JAMBA_EXPIRINGDATACACHE_H
20 #define JAMBA_EXPIRINGDATACACHE_H
21 
22 #include <pongasoft/VST/Timer.h>
23 #include <functional>
24 
25 #include <pongasoft/logging/logging.h>
26 
28 
29 using namespace Steinberg;
30 
70 template<typename T, typename Ptr = std::shared_ptr<T>, typename Loader = std::function<Ptr()>>
71 class ExpiringDataCache : ITimerCallback
72 {
73 public:
74  using value_type = T;
75  using pointer = Ptr;
76  using loader = Loader;
77 
78 public:
80  ExpiringDataCache() = default;
81 
83  ExpiringDataCache(Loader iDataLoader, uint32 iTimeToLiveMilliseconds) :
84  fDataLoader{std::move(iDataLoader)}, fTimeToLiveMilliseconds{iTimeToLiveMilliseconds} {}
85 
88  fDataLoader{iOther.fDataLoader}, fTimeToLiveMilliseconds{iOther.fTimeToLiveMilliseconds} {}
89 
91  ExpiringDataCache(ExpiringDataCache &&iOther) noexcept :
92  fDataLoader{std::move(iOther.fDataLoader)}, fTimeToLiveMilliseconds{iOther.fTimeToLiveMilliseconds} {}
93 
96  {
97  fDataLoader = iOther.fDataLoader;
98  fTimeToLiveMilliseconds = iOther.fTimeToLiveMilliseconds;
99  fCachedData = nullptr;
100  fTimer = nullptr;
101 
102  return *this;
103  }
104 
107  {
108  fDataLoader = std::move(iOther.fDataLoader);
109  fTimeToLiveMilliseconds = iOther.fTimeToLiveMilliseconds;
110  fCachedData = nullptr;
111  fTimer = nullptr;
112 
113  return *this;
114  }
115 
117  explicit operator bool() const noexcept { return fDataLoader ? true : false; }
118 
126  Ptr getData()
127  {
128  if(fCachedData)
129  {
130  // resets the timer
131  fTimer = AutoReleaseTimer::create(this, fTimeToLiveMilliseconds);
132  return fCachedData;
133  }
134 
135  fCachedData = fDataLoader ? fDataLoader() : nullptr;
136 
137  if(fCachedData)
138  fTimer = AutoReleaseTimer::create(this, fTimeToLiveMilliseconds);
139 
140  return fCachedData;
141  }
142 
143 private:
144  // Callback when the timer expires => remove cached data
145  void onTimer(Timer *timer) override
146  {
147  fCachedData = nullptr;
148  fTimer = nullptr;
149  }
150 
151 private:
152  Loader fDataLoader{};
153  uint32 fTimeToLiveMilliseconds{};
154 
155  Ptr fCachedData{};
156  std::unique_ptr<AutoReleaseTimer> fTimer{};
157 };
158 
159 }
160 
161 #endif //JAMBA_EXPIRINGDATACACHE_H
Ptr pointer
Definition: ExpiringDataCache.h:75
Loader fDataLoader
Definition: ExpiringDataCache.h:152
T value_type
Definition: ExpiringDataCache.h:74
ExpiringDataCache(Loader iDataLoader, uint32 iTimeToLiveMilliseconds)
Main constructor with loader and TTL.
Definition: ExpiringDataCache.h:83
ExpiringDataCache(ExpiringDataCache const &iOther)
Copy constructor.
Definition: ExpiringDataCache.h:87
Definition: ExpiringDataCache.h:27
Loader loader
Definition: ExpiringDataCache.h:76
void onTimer(Timer *timer) override
Definition: ExpiringDataCache.h:145
Ptr getData()
Main api to retrieve the data.
Definition: ExpiringDataCache.h:126
ExpiringDataCache(ExpiringDataCache &&iOther) noexcept
Move constructor.
Definition: ExpiringDataCache.h:91
The purpose of this class is to implement a short live cache (time to live (or TTL) being a construct...
Definition: ExpiringDataCache.h:71
ExpiringDataCache & operator=(ExpiringDataCache &&iOther) noexcept
Move assignment operator.
Definition: ExpiringDataCache.h:106
static std::unique_ptr< AutoReleaseTimer > create(Steinberg::ITimerCallback *iCallback, Steinberg::uint32 iIntervalMilliseconds)
Creates and return an auto release timer.
Definition: Timer.h:54
ExpiringDataCache & operator=(ExpiringDataCache const &iOther) noexcept
Copy assignment operator.
Definition: ExpiringDataCache.h:95