Hazelcast C++ Client
NearCacheManager.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_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/internal/adapter/DataStructureAdapter.h"
26 #include "hazelcast/client/serialization/pimpl/SerializationService.h"
27 
28 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
29 #pragma warning(push)
30 #pragma warning(disable: 4251) //for dll export
31 #endif
32 
33 namespace hazelcast {
34  namespace client {
35  namespace internal {
36  namespace nearcache {
40  class HAZELCAST_API NearCacheManager {
41  public:
42  NearCacheManager(serialization::pimpl::SerializationService &ss)
43  : serializationService(ss) {
44  }
45 
55  template<typename K, typename V, typename KS>
56  boost::shared_ptr<NearCache<KS, V> > getNearCache(const std::string &name) {
57  return boost::static_pointer_cast<NearCache<KS, V> >(nearCacheMap.get(name));
58  };
59 
74  template<typename K, typename V, typename KS>
75  boost::shared_ptr<NearCache<KS, V> > getOrCreateNearCache(
76  const std::string &name, const config::NearCacheConfig<K, V> &nearCacheConfig,
77  std::auto_ptr<adapter::DataStructureAdapter<K, V> > &dataStructureAdapter) {
78  boost::shared_ptr<BaseNearCache> nearCache = nearCacheMap.get(name);
79  if (NULL == nearCache.get()) {
80  {
81  util::LockGuard guard(mutex);
82  nearCache = nearCacheMap.get(name);
83  if (NULL == nearCache.get()) {
84  nearCache = createNearCache<K, V, KS>(name, nearCacheConfig);
85  nearCache->initialize();
86 
87  nearCacheMap.put(name, nearCache);
88  }
89 
90  }
91  }
92  return boost::static_pointer_cast<NearCache<KS, V> >(nearCache);
93  }
94 
100 /*
101  virtual std::vector<boost::shared_ptr<NearCache<K, V> > > listAllNearCaches() const = 0;
102 */
103 
110  bool clearNearCache(const std::string &name) {
111  boost::shared_ptr<BaseNearCache> nearCache = nearCacheMap.get(name);
112  if (nearCache.get() != NULL) {
113  nearCache->clear();
114  }
115  return nearCache.get() != NULL;
116  }
117 
122  std::vector<boost::shared_ptr<BaseNearCache> > caches = nearCacheMap.values();
123  for (std::vector<boost::shared_ptr<BaseNearCache> >::iterator it = caches.begin();
124  it != caches.end(); ++it) {
125  (*it)->clear();
126  }
127  }
128 
135  bool destroyNearCache(const std::string &name) {
136  boost::shared_ptr<BaseNearCache> nearCache = nearCacheMap.remove(name);
137  if (nearCache.get() != NULL) {
138  nearCache->destroy();
139  }
140  return nearCache.get() != NULL;
141  }
142 
147  std::vector<boost::shared_ptr<BaseNearCache> > caches = nearCacheMap.values();
148  for (std::vector<boost::shared_ptr<BaseNearCache> >::iterator it = caches.begin();
149  it != caches.end(); ++it) {
150  (*it)->destroy();
151  }
152  }
153  protected:
154  template<typename K, typename V, typename KS>
155  std::auto_ptr<NearCache<KS, V> > createNearCache(
156  const std::string &name, const config::NearCacheConfig<K, V> &nearCacheConfig) {
157  return std::auto_ptr<NearCache<KS, V> >(
158  new impl::DefaultNearCache<K, V, KS>(
159  name, nearCacheConfig, serializationService));
160  }
161  private:
162  serialization::pimpl::SerializationService &serializationService;
163  util::SynchronizedMap<std::string, BaseNearCache> nearCacheMap;
164  util::Mutex mutex;
165  };
166  }
167  }
168  }
169 };
170 
171 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
172 #pragma warning(pop)
173 #endif
174 
175 #endif /* HAZELCAST_CLIENT_INTERNAL_NEARCACHE_NEARCACHEMANAGER_H_ */
void destroyAllNearCaches()
Destroys all defined NearCache instances.
Definition: NearCacheManager.h:146
Contains the configuration for a Near Cache.
Definition: NearCacheConfig.h:44
Abstracts the Hazelcast data structures with Near Cache support for the Near Cache usage...
Definition: DataStructureAdapter.h:37
bool destroyNearCache(const std::string &name)
Destroys NearCache instance associated with given.
Definition: NearCacheManager.h:135
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:56
NearCacheManager is the contract point to manage all existing NearCache instances.
Definition: NearCacheManager.h:40
void clearAllNearCaches()
Clears all defined NearCache instances.
Definition: NearCacheManager.h:121
Definition: MapEntryView.h:32
boost::shared_ptr< NearCache< KS, V > > getOrCreateNearCache(const std::string &name, const config::NearCacheConfig< K, V > &nearCacheConfig, std::auto_ptr< adapter::DataStructureAdapter< K, V > > &dataStructureAdapter)
Creates a new NearCache with given configurations or returns existing one.
Definition: NearCacheManager.h:75
bool clearNearCache(const std::string &name)
Lists all existing NearCache instances.
Definition: NearCacheManager.h:110