Hazelcast C++ Client
NearCacheManager.h
1 /*
2  * Copyright (c) 2008-2018, 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_INTERNAL_NEARCACHE_NEARCACHEMANAGER_H_
17 #define HAZELCAST_CLIENT_INTERNAL_NEARCACHE_NEARCACHEMANAGER_H_
18 
19 #include <string>
20 #include <vector>
21 
22 #include "hazelcast/util/HazelcastDll.h"
23 #include "hazelcast/client/internal/nearcache/NearCache.h"
24 #include "hazelcast/client/internal/nearcache/impl/DefaultNearCache.h"
25 #include "hazelcast/client/serialization/pimpl/SerializationService.h"
26 
27 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
28 #pragma warning(push)
29 #pragma warning(disable: 4251) //for dll export
30 #endif
31 
32 namespace hazelcast {
33  namespace client {
34  namespace internal {
35  namespace nearcache {
39  class HAZELCAST_API NearCacheManager {
40  public:
41  NearCacheManager(serialization::pimpl::SerializationService &ss)
42  : serializationService(ss) {
43  }
44 
54  template<typename K, typename V, typename KS>
55  boost::shared_ptr<NearCache<KS, V> > getNearCache(const std::string &name) {
56  return boost::static_pointer_cast<NearCache<KS, V> >(nearCacheMap.get(name));
57  };
58 
71  template<typename K, typename V, typename KS>
72  boost::shared_ptr<NearCache<KS, V> > getOrCreateNearCache(
73  const std::string &name, const config::NearCacheConfig<K, V> &nearCacheConfig) {
74  boost::shared_ptr<BaseNearCache> nearCache = nearCacheMap.get(name);
75  if (NULL == nearCache.get()) {
76  {
77  util::LockGuard guard(mutex);
78  nearCache = nearCacheMap.get(name);
79  if (NULL == nearCache.get()) {
80  nearCache = createNearCache<K, V, KS>(name, nearCacheConfig);
81  nearCache->initialize();
82 
83  nearCacheMap.put(name, nearCache);
84  }
85 
86  }
87  }
88  return boost::static_pointer_cast<NearCache<KS, V> >(nearCache);
89  }
90 
96 /*
97  virtual std::vector<boost::shared_ptr<NearCache<K, V> > > listAllNearCaches() const = 0;
98 */
99 
106  bool clearNearCache(const std::string &name) {
107  boost::shared_ptr<BaseNearCache> nearCache = nearCacheMap.get(name);
108  if (nearCache.get() != NULL) {
109  nearCache->clear();
110  }
111  return nearCache.get() != NULL;
112  }
113 
118  std::vector<boost::shared_ptr<BaseNearCache> > caches = nearCacheMap.values();
119  for (std::vector<boost::shared_ptr<BaseNearCache> >::iterator it = caches.begin();
120  it != caches.end(); ++it) {
121  (*it)->clear();
122  }
123  }
124 
131  bool destroyNearCache(const std::string &name) {
132  boost::shared_ptr<BaseNearCache> nearCache = nearCacheMap.remove(name);
133  if (nearCache.get() != NULL) {
134  nearCache->destroy();
135  }
136  return nearCache.get() != NULL;
137  }
138 
143  std::vector<boost::shared_ptr<BaseNearCache> > caches = nearCacheMap.values();
144  for (std::vector<boost::shared_ptr<BaseNearCache> >::iterator it = caches.begin();
145  it != caches.end(); ++it) {
146  (*it)->destroy();
147  }
148  }
149  protected:
150  template<typename K, typename V, typename KS>
151  std::auto_ptr<NearCache<KS, V> > createNearCache(
152  const std::string &name, const config::NearCacheConfig<K, V> &nearCacheConfig) {
153  return std::auto_ptr<NearCache<KS, V> >(
154  new impl::DefaultNearCache<K, V, KS>(
155  name, nearCacheConfig, serializationService));
156  }
157  private:
158  serialization::pimpl::SerializationService &serializationService;
159  util::SynchronizedMap<std::string, BaseNearCache> nearCacheMap;
160  util::Mutex mutex;
161  };
162  }
163  }
164  }
165 };
166 
167 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
168 #pragma warning(pop)
169 #endif
170 
171 #endif /* HAZELCAST_CLIENT_INTERNAL_NEARCACHE_NEARCACHEMANAGER_H_ */
void destroyAllNearCaches()
Destroys all defined NearCache instances.
Definition: NearCacheManager.h:142
Contains the configuration for a Near Cache.
Definition: NearCacheConfig.h:44
bool destroyNearCache(const std::string &name)
Destroys NearCache instance associated with given.
Definition: NearCacheManager.h:131
NearCache is the contract point to store keys and values in underlying com.hazelcast.cache.impl.nearcache.NearCacheRecordStore.
Definition: NearCache.h:68
boost::shared_ptr< NearCache< KS, V > > getNearCache(const std::string &name)
Gets the NearCache instance associated with given.
Definition: NearCacheManager.h:55
NearCacheManager is the contract point to manage all existing NearCache instances.
Definition: NearCacheManager.h:39
void clearAllNearCaches()
Clears all defined NearCache instances.
Definition: NearCacheManager.h:117
Definition: MapEntryView.h:32
bool clearNearCache(const std::string &name)
Lists all existing NearCache instances.
Definition: NearCacheManager.h:106
boost::shared_ptr< NearCache< KS, V > > getOrCreateNearCache(const std::string &name, const config::NearCacheConfig< K, V > &nearCacheConfig)
Creates a new NearCache with given configurations or returns existing one.
Definition: NearCacheManager.h:72