Hazelcast C++ Client
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 #include "hazelcast/client/serialization/pimpl/Data.h"
29 #include "hazelcast/client/TypedData.h"
30 
31 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
32 #pragma warning(push)
33 #pragma warning(disable: 4251) //for dll export
34 #endif
35 
36 namespace hazelcast {
37  namespace client {
38  namespace config {
43  template <typename K, typename V>
45  public:
49  static const int32_t DEFAULT_TTL_SECONDS;
50 
54  static const int32_t DEFAULT_MAX_IDLE_SECONDS;
55 
59  static const InMemoryFormat DEFAULT_MEMORY_FORMAT;
60 
69 
74  };
75 
76  NearCacheConfig() : name("default"), timeToLiveSeconds(DEFAULT_TTL_SECONDS),
77  maxIdleSeconds(DEFAULT_MAX_IDLE_SECONDS),
78  inMemoryFormat(DEFAULT_MEMORY_FORMAT),
79  localUpdatePolicy(INVALIDATE), invalidateOnChange(true), cacheLocalEntries(false),
80  evictionConfig(new EvictionConfig<K, V>()) {
81  }
82 
83  NearCacheConfig(const char *cacheName) : name(cacheName), timeToLiveSeconds(DEFAULT_TTL_SECONDS),
84  maxIdleSeconds(DEFAULT_MAX_IDLE_SECONDS),
85  inMemoryFormat(DEFAULT_MEMORY_FORMAT),
86  localUpdatePolicy(INVALIDATE), invalidateOnChange(true),
87  cacheLocalEntries(false),
88  evictionConfig(new EvictionConfig<K, V>()) {
89  }
90 
91  NearCacheConfig(const char *cacheName, InMemoryFormat memoryFormat) : name(cacheName), timeToLiveSeconds(DEFAULT_TTL_SECONDS),
92  maxIdleSeconds(DEFAULT_MAX_IDLE_SECONDS),
93  inMemoryFormat(memoryFormat),
94  localUpdatePolicy(INVALIDATE), invalidateOnChange(true),
95  cacheLocalEntries(false),
96  evictionConfig(new EvictionConfig<K, V>()) {
97  }
98 
99  NearCacheConfig(int32_t timeToLiveSeconds, int32_t maxIdleSeconds, bool invalidateOnChange,
100  InMemoryFormat inMemoryFormat, boost::shared_ptr<EvictionConfig<K, V> > evictConfig)
101  : evictionConfig(new EvictionConfig<K, V>()) {
102  this->timeToLiveSeconds = timeToLiveSeconds;
103  this->maxIdleSeconds = maxIdleSeconds;
104  this->invalidateOnChange = invalidateOnChange;
105  this->inMemoryFormat = inMemoryFormat;
106  localUpdatePolicy= INVALIDATE;
107  // EvictionConfig is not allowed to be NULL
108  if (evictConfig.get() != NULL) {
109  this->evictionConfig = evictConfig;
110  }
111  this->cacheLocalEntries = false;
112  }
113 
114  NearCacheConfig(const NearCacheConfig<K, V> &config) {
115  name = config.getName();
116  inMemoryFormat = config.getInMemoryFormat();
117  invalidateOnChange = config.isInvalidateOnChange();
118  maxIdleSeconds = config.getMaxIdleSeconds();
119  timeToLiveSeconds = config.getTimeToLiveSeconds();
120  cacheLocalEntries = config.isCacheLocalEntries();
121  localUpdatePolicy = config.localUpdatePolicy;
122  // EvictionConfig is not allowed to be NULL
123  if (config.evictionConfig.get() != NULL) {
124  this->evictionConfig = config.evictionConfig;
125  }
126  }
127 
128  virtual ~NearCacheConfig() {
129  }
130 
136  const std::string &getName() const {
137  return name;
138  }
139 
146  NearCacheConfig &setName(const std::string &name) {
147  this->name = name;
148  return *this;
149  }
150 
157  int32_t getTimeToLiveSeconds() const {
158  return timeToLiveSeconds;
159  }
160 
169  NearCacheConfig &setTimeToLiveSeconds(int32_t timeToLiveSeconds) {
170  this->timeToLiveSeconds = util::Preconditions::checkNotNegative(timeToLiveSeconds,
171  "TTL seconds cannot be negative!");
172  return *this;
173  }
174 
183  int32_t getMaxIdleSeconds() const {
184  return maxIdleSeconds;
185  }
186 
197  NearCacheConfig &setMaxIdleSeconds(int32_t maxIdleSeconds) {
198  this->maxIdleSeconds = util::Preconditions::checkNotNegative(maxIdleSeconds,
199  "Max-Idle seconds cannot be negative!");
200  return *this;
201  }
202 
211  bool isInvalidateOnChange() const {
212  return invalidateOnChange;
213  }
214 
225  NearCacheConfig &setInvalidateOnChange(bool invalidateOnChange) {
226  this->invalidateOnChange = invalidateOnChange;
227  return *this;
228  }
229 
238  const InMemoryFormat &getInMemoryFormat() const {
239  return inMemoryFormat;
240  }
241 
251  virtual NearCacheConfig &setInMemoryFormat(const InMemoryFormat &inMemoryFormat) {
252  this->inMemoryFormat = inMemoryFormat;
253  return *this;
254  }
255 
262  bool isCacheLocalEntries() const {
263  return cacheLocalEntries;
264  }
265 
273  NearCacheConfig &setCacheLocalEntries(bool cacheLocalEntries) {
274  this->cacheLocalEntries = cacheLocalEntries;
275  return *this;
276  }
277 
278  const LocalUpdatePolicy &getLocalUpdatePolicy() const {
279  return localUpdatePolicy;
280  }
281 
282  NearCacheConfig &setLocalUpdatePolicy(const LocalUpdatePolicy &localUpdatePolicy) {
283  this->localUpdatePolicy = localUpdatePolicy;
284  return *this;
285  }
286 
292  const boost::shared_ptr<EvictionConfig<K, V> > &getEvictionConfig() const {
293  return evictionConfig;
294  }
295 
302  NearCacheConfig &setEvictionConfig(const boost::shared_ptr<EvictionConfig<K, V> > &evictionConfig) {
303  this->evictionConfig = util::Preconditions::checkNotNull<EvictionConfig<K, V> >(evictionConfig,
304  "EvictionConfig cannot be NULL!");
305  return *this;
306  }
307 
308  std::ostream &operator<<(std::ostream &out) {
309  out << "NearCacheConfig{"
310  << "timeToLiveSeconds=" << timeToLiveSeconds
311  << ", maxIdleSeconds=" << maxIdleSeconds
312  << ", invalidateOnChange=" << invalidateOnChange
313  << ", inMemoryFormat=" << inMemoryFormat
314  << ", cacheLocalEntries=" << cacheLocalEntries
315  << ", localUpdatePolicy=" << localUpdatePolicy
316  << *evictionConfig;
317  out << '}';
318 
319  return out;
320  }
321  private:
322  std::string name;
323 
324  int32_t timeToLiveSeconds;
325  int32_t maxIdleSeconds;
326 
327  InMemoryFormat inMemoryFormat;
328 
329  LocalUpdatePolicy localUpdatePolicy;
330 
331  bool invalidateOnChange;
332  bool cacheLocalEntries;
333 
342  boost::shared_ptr<EvictionConfig<K, V> > evictionConfig;
343 
344  int32_t calculateMaxSize(int32_t maxSize) {
345  return (maxSize == 0) ? INT32_MAX : util::Preconditions::checkNotNegative(maxSize,
346  "Max-size cannot be negative!");
347  }
348  };
349 
350  template<typename K, typename V>
351  const int32_t NearCacheConfig<K, V>::DEFAULT_TTL_SECONDS = 0;
352 
353  template<typename K, typename V>
354  const int32_t NearCacheConfig<K, V>::DEFAULT_MAX_IDLE_SECONDS = 0;
355 
356  template<typename K, typename V>
357  const InMemoryFormat NearCacheConfig<K, V>::DEFAULT_MEMORY_FORMAT = BINARY;
358 
359  }
360 
361  namespace mixedtype {
362  namespace config {
363  class HAZELCAST_API MixedNearCacheConfig : public client::config::NearCacheConfig<TypedData, TypedData> {
364  public:
365  MixedNearCacheConfig(const char *cacheName)
367  }
368 
369  virtual MixedNearCacheConfig &setInMemoryFormat(const client::config::InMemoryFormat &inMemoryFormat) {
370  if (client::config::OBJECT == inMemoryFormat) {
371  throw exception::IllegalArgumentException(
372  "MixedNearCacheConfig does not allow setting the in memory format different from BINARY.");
373  }
374 
376  return *this;
377  }
378  };
379  }
380  }
381  }
382 }
383 
384 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
385 #pragma warning(pop)
386 #endif
387 
388 #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:211
Configuration for eviction.
Definition: EvictionConfig.h:43
bool isCacheLocalEntries() const
If true, cache local entries also.
Definition: NearCacheConfig.h:262
NearCacheConfig & setInvalidateOnChange(bool invalidateOnChange)
True to evict the cached entries if the entries are changed (updated or removed). ...
Definition: NearCacheConfig.h:225
static const InMemoryFormat DEFAULT_MEMORY_FORMAT
Default value for the in-memory format.
Definition: NearCacheConfig.h:59
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:44
NearCacheConfig & setMaxIdleSeconds(int32_t maxIdleSeconds)
Maximum number of seconds each entry can stay in the Near Cache as untouched (not-read).
Definition: NearCacheConfig.h:197
LocalUpdatePolicy
Local Update Policy enum.
Definition: NearCacheConfig.h:64
int32_t getMaxIdleSeconds() const
Maximum number of seconds each entry can stay in the Near Cache as untouched (not-read).
Definition: NearCacheConfig.h:183
INVALIDATE POLICY.
Definition: NearCacheConfig.h:68
const std::string & getName() const
Gets the name of the Near Cache.
Definition: NearCacheConfig.h:136
static const int32_t DEFAULT_MAX_IDLE_SECONDS
Default value of the idle time for eviction in seconds.
Definition: NearCacheConfig.h:54
NearCacheConfig & setEvictionConfig(const boost::shared_ptr< EvictionConfig< K, V > > &evictionConfig)
Sets the eviction configuration.
Definition: NearCacheConfig.h:302
NearCacheConfig & setCacheLocalEntries(bool cacheLocalEntries)
True to cache local entries also.
Definition: NearCacheConfig.h:273
virtual NearCacheConfig & setInMemoryFormat(const InMemoryFormat &inMemoryFormat)
Sets the data type used to store entries.
Definition: NearCacheConfig.h:251
NearCacheConfig & setName(const std::string &name)
Sets the name of the Near Cache.
Definition: NearCacheConfig.h:146
int32_t getTimeToLiveSeconds() const
Gets the maximum number of seconds for each entry to stay in the Near Cache.
Definition: NearCacheConfig.h:157
CACHE ON UPDATE POLICY.
Definition: NearCacheConfig.h:73
Definition: MapEntryView.h:32
static const int32_t DEFAULT_TTL_SECONDS
Default value of the time to live in seconds.
Definition: NearCacheConfig.h:49
const InMemoryFormat & getInMemoryFormat() const
Gets the data type used to store entries.
Definition: NearCacheConfig.h:238
NearCacheConfig & setTimeToLiveSeconds(int32_t timeToLiveSeconds)
Sets the maximum number of seconds for each entry to stay in the Near Cache.
Definition: NearCacheConfig.h:169
const boost::shared_ptr< EvictionConfig< K, V > > & getEvictionConfig() const
The eviction configuration.
Definition: NearCacheConfig.h:292