Hazelcast C++ Client
 All Classes Functions Variables Enumerations Enumerator Pages
NearCacheConfig.h
1 /*
2  * Copyright (c) 2008-2017, Hazelcast, Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef HAZELCAST_CLIENT_CONFIG_NEARCACHECONFIG_H_
17 #define HAZELCAST_CLIENT_CONFIG_NEARCACHECONFIG_H_
18 
19 #include <string>
20 #include <sstream>
21 #include <stdint.h>
22 
23 #include <boost/shared_ptr.hpp>
24 
25 #include "hazelcast/client/config/InMemoryFormat.h"
26 #include "hazelcast/client/config/EvictionConfig.h"
27 #include "hazelcast/client/config/NearCacheConfigBase.h"
28 
29 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
30 #pragma warning(push)
31 #pragma warning(disable: 4251) //for dll export
32 #endif
33 
34 namespace hazelcast {
35  namespace client {
36  namespace config {
41  template <typename K, typename V>
43  public:
47  static const int32_t DEFAULT_TTL_SECONDS;
48 
52  static const int32_t DEFAULT_MAX_IDLE_SECONDS;
53 
57  static const InMemoryFormat DEFAULT_MEMORY_FORMAT;
58 
67 
72  };
73 
74  NearCacheConfig() : name("default"), timeToLiveSeconds(DEFAULT_TTL_SECONDS),
75  maxIdleSeconds(DEFAULT_MAX_IDLE_SECONDS),
76  inMemoryFormat(DEFAULT_MEMORY_FORMAT),
77  localUpdatePolicy(INVALIDATE), invalidateOnChange(true), cacheLocalEntries(false),
78  evictionConfig(new EvictionConfig<K, V>()) {
79  }
80 
81  NearCacheConfig(const char *cacheName) : name(cacheName), timeToLiveSeconds(DEFAULT_TTL_SECONDS),
82  maxIdleSeconds(DEFAULT_MAX_IDLE_SECONDS),
83  inMemoryFormat(DEFAULT_MEMORY_FORMAT),
84  localUpdatePolicy(INVALIDATE), invalidateOnChange(true),
85  cacheLocalEntries(false),
86  evictionConfig(new EvictionConfig<K, V>()) {
87  }
88 
89  NearCacheConfig(const char *cacheName, InMemoryFormat memoryFormat) : name(cacheName), timeToLiveSeconds(DEFAULT_TTL_SECONDS),
90  maxIdleSeconds(DEFAULT_MAX_IDLE_SECONDS),
91  inMemoryFormat(memoryFormat),
92  localUpdatePolicy(INVALIDATE), invalidateOnChange(true),
93  cacheLocalEntries(false),
94  evictionConfig(new EvictionConfig<K, V>()) {
95  }
96 
97  NearCacheConfig(int32_t timeToLiveSeconds, int32_t maxIdleSeconds, bool invalidateOnChange,
98  InMemoryFormat inMemoryFormat, boost::shared_ptr<EvictionConfig<K, V> > evictConfig)
99  : evictionConfig(new EvictionConfig<K, V>()) {
100  this->timeToLiveSeconds = timeToLiveSeconds;
101  this->maxIdleSeconds = maxIdleSeconds;
102  this->invalidateOnChange = invalidateOnChange;
103  this->inMemoryFormat = inMemoryFormat;
104  localUpdatePolicy= INVALIDATE;
105  // EvictionConfig is not allowed to be NULL
106  if (evictConfig.get() != NULL) {
107  this->evictionConfig = evictConfig;
108  }
109  this->cacheLocalEntries = false;
110  }
111 
112  NearCacheConfig(const NearCacheConfig<K, V> &config) {
113  name = config.getName();
114  inMemoryFormat = config.getInMemoryFormat();
115  invalidateOnChange = config.isInvalidateOnChange();
116  maxIdleSeconds = config.getMaxIdleSeconds();
117  timeToLiveSeconds = config.getTimeToLiveSeconds();
118  cacheLocalEntries = config.isCacheLocalEntries();
119  localUpdatePolicy = config.localUpdatePolicy;
120  // EvictionConfig is not allowed to be NULL
121  if (config.evictionConfig.get() != NULL) {
122  this->evictionConfig = config.evictionConfig;
123  }
124  }
125 
131  const std::string &getName() const {
132  return name;
133  }
134 
141  NearCacheConfig &setName(const std::string &name) {
142  this->name = name;
143  return *this;
144  }
145 
152  int32_t getTimeToLiveSeconds() const {
153  return timeToLiveSeconds;
154  }
155 
164  NearCacheConfig &setTimeToLiveSeconds(int32_t timeToLiveSeconds) {
165  this->timeToLiveSeconds = util::Preconditions::checkNotNegative(timeToLiveSeconds,
166  "TTL seconds cannot be negative!");
167  return *this;
168  }
169 
178  int32_t getMaxIdleSeconds() const {
179  return maxIdleSeconds;
180  }
181 
192  NearCacheConfig &setMaxIdleSeconds(int32_t maxIdleSeconds) {
193  this->maxIdleSeconds = util::Preconditions::checkNotNegative(maxIdleSeconds,
194  "Max-Idle seconds cannot be negative!");
195  return *this;
196  }
197 
206  bool isInvalidateOnChange() const {
207  return invalidateOnChange;
208  }
209 
220  NearCacheConfig &setInvalidateOnChange(bool invalidateOnChange) {
221  this->invalidateOnChange = invalidateOnChange;
222  return *this;
223  }
224 
233  const InMemoryFormat &getInMemoryFormat() const {
234  return inMemoryFormat;
235  }
236 
246  NearCacheConfig &setInMemoryFormat(const InMemoryFormat &inMemoryFormat) {
247  this->inMemoryFormat = inMemoryFormat;
248  return *this;
249  }
250 
257  bool isCacheLocalEntries() const {
258  return cacheLocalEntries;
259  }
260 
268  NearCacheConfig &setCacheLocalEntries(bool cacheLocalEntries) {
269  this->cacheLocalEntries = cacheLocalEntries;
270  return *this;
271  }
272 
273  const LocalUpdatePolicy &getLocalUpdatePolicy() const {
274  return localUpdatePolicy;
275  }
276 
277  NearCacheConfig &setLocalUpdatePolicy(const LocalUpdatePolicy &localUpdatePolicy) {
278  this->localUpdatePolicy = localUpdatePolicy;
279  return *this;
280  }
281 
287  const boost::shared_ptr<EvictionConfig<K, V> > &getEvictionConfig() const {
288  return evictionConfig;
289  }
290 
297  NearCacheConfig &setEvictionConfig(const boost::shared_ptr<EvictionConfig<K, V> > &evictionConfig) {
298  this->evictionConfig = util::Preconditions::checkNotNull<EvictionConfig<K, V> >(evictionConfig,
299  "EvictionConfig cannot be NULL!");
300  return *this;
301  }
302 
303  std::ostream &operator<<(std::ostream &out) {
304  out << "NearCacheConfig{"
305  << "timeToLiveSeconds=" << timeToLiveSeconds
306  << ", maxIdleSeconds=" << maxIdleSeconds
307  << ", invalidateOnChange=" << invalidateOnChange
308  << ", inMemoryFormat=" << inMemoryFormat
309  << ", cacheLocalEntries=" << cacheLocalEntries
310  << ", localUpdatePolicy=" << localUpdatePolicy
311  << *evictionConfig;
312  out << '}';
313 
314  return out;
315  }
316  private:
317  std::string name;
318 
319  int32_t timeToLiveSeconds;
320  int32_t maxIdleSeconds;
321 
322  InMemoryFormat inMemoryFormat;
323 
324  LocalUpdatePolicy localUpdatePolicy;
325 
326  bool invalidateOnChange;
327  bool cacheLocalEntries;
328 
337  boost::shared_ptr<EvictionConfig<K, V> > evictionConfig;
338 
339  int32_t calculateMaxSize(int32_t maxSize) {
340  return (maxSize == 0) ? INT32_MAX : util::Preconditions::checkNotNegative(maxSize,
341  "Max-size cannot be negative!");
342  }
343  };
344 
345  template<typename K, typename V>
346  const int32_t NearCacheConfig<K, V>::DEFAULT_TTL_SECONDS = 0;
347 
348  template<typename K, typename V>
349  const int32_t NearCacheConfig<K, V>::DEFAULT_MAX_IDLE_SECONDS = 0;
350 
351  template<typename K, typename V>
352  const InMemoryFormat NearCacheConfig<K, V>::DEFAULT_MEMORY_FORMAT = BINARY;
353  }
354  }
355 }
356 
357 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
358 #pragma warning(pop)
359 #endif
360 
361 #endif /* HAZELCAST_CLIENT_CONFIG_NEARCACHECONFIG_H_ */
bool isInvalidateOnChange() const
True to evict the cached entries if the entries are changed (updated or removed). ...
Definition: NearCacheConfig.h:206
Configuration for eviction.
Definition: EvictionConfig.h:42
bool isCacheLocalEntries() const
If true, cache local entries also.
Definition: NearCacheConfig.h:257
NearCacheConfig & setInvalidateOnChange(bool invalidateOnChange)
True to evict the cached entries if the entries are changed (updated or removed). ...
Definition: NearCacheConfig.h:220
static const InMemoryFormat DEFAULT_MEMORY_FORMAT
Default value for the in-memory format.
Definition: NearCacheConfig.h:57
This is a marker class to indicate that the derived class is a near cache config. ...
Definition: NearCacheConfigBase.h:32
Contains the configuration for a Near Cache.
Definition: NearCacheConfig.h:42
NearCacheConfig & setMaxIdleSeconds(int32_t maxIdleSeconds)
Maximum number of seconds each entry can stay in the Near Cache as untouched (not-read).
Definition: NearCacheConfig.h:192
LocalUpdatePolicy
Local Update Policy enum.
Definition: NearCacheConfig.h:62
int32_t getMaxIdleSeconds() const
Maximum number of seconds each entry can stay in the Near Cache as untouched (not-read).
Definition: NearCacheConfig.h:178
INVALIDATE POLICY.
Definition: NearCacheConfig.h:66
const std::string & getName() const
Gets the name of the Near Cache.
Definition: NearCacheConfig.h:131
static const int32_t DEFAULT_MAX_IDLE_SECONDS
Default value of the idle time for eviction in seconds.
Definition: NearCacheConfig.h:52
NearCacheConfig & setEvictionConfig(const boost::shared_ptr< EvictionConfig< K, V > > &evictionConfig)
Sets the eviction configuration.
Definition: NearCacheConfig.h:297
NearCacheConfig & setCacheLocalEntries(bool cacheLocalEntries)
True to cache local entries also.
Definition: NearCacheConfig.h:268
NearCacheConfig & setName(const std::string &name)
Sets the name of the Near Cache.
Definition: NearCacheConfig.h:141
int32_t getTimeToLiveSeconds() const
Gets the maximum number of seconds for each entry to stay in the Near Cache.
Definition: NearCacheConfig.h:152
CACHE ON UPDATE POLICY.
Definition: NearCacheConfig.h:71
static const int32_t DEFAULT_TTL_SECONDS
Default value of the time to live in seconds.
Definition: NearCacheConfig.h:47
const InMemoryFormat & getInMemoryFormat() const
Gets the data type used to store entries.
Definition: NearCacheConfig.h:233
NearCacheConfig & setTimeToLiveSeconds(int32_t timeToLiveSeconds)
Sets the maximum number of seconds for each entry to stay in the Near Cache.
Definition: NearCacheConfig.h:164
const boost::shared_ptr< EvictionConfig< K, V > > & getEvictionConfig() const
The eviction configuration.
Definition: NearCacheConfig.h:287
NearCacheConfig & setInMemoryFormat(const InMemoryFormat &inMemoryFormat)
Sets the data type used to store entries.
Definition: NearCacheConfig.h:246