|
MemCacheClient
|
00001 00090 #ifndef INCLUDED_MemCacheClient 00091 #define INCLUDED_MemCacheClient 00092 00093 #if defined(_MSC_VER) && (_MSC_VER >= 1020) 00094 # pragma once 00095 #endif 00096 00097 #ifndef WIN32 00098 # include <stdint.h> 00099 #endif 00100 00101 #include <string> 00102 #include <vector> 00103 00104 #ifdef CROSSBASE_API 00105 #include <Core/ReadWriteBuffer.h> 00106 START_CL_NAMESPACE 00107 #else 00108 # include "ReadWriteBuffer.h" 00109 # include "Matilda.h" 00110 #endif 00111 00112 // ---------------------------------------------------------------------------- 00118 enum MCResult { 00119 // success: n >= 0 00120 MCERR_OK = 0, 00121 MCERR_NOREPLY = 1, 00122 MCERR_NOTSTORED = 2, 00123 MCERR_NOTFOUND = 3, 00124 00125 // failure: n < 0 00126 MCERR_NOSERVER = -1 00127 }; 00128 00129 // ---------------------------------------------------------------------------- 00137 class CROSSBASE_CLASS_API MemCacheClient 00138 { 00139 class Server; 00140 00141 public: 00142 typedef std::string string_t; 00143 #ifdef WIN32 00144 typedef unsigned __int64 uint64_t; 00145 #else 00146 typedef unsigned long long uint64_t; 00147 #endif 00148 00150 struct MemRequest 00151 { 00152 struct Sort; 00153 Server * mServer; 00154 00155 public: 00157 MemRequest() { Clear(); } 00158 00160 void Clear() { 00161 mServer = NULL; 00162 mService = 0; 00163 mKey = ""; 00164 mFlags = 0; 00165 mExpiry = 0; 00166 mCas = 0; 00167 mResult = MCERR_OK; 00168 mData.SetEmpty(); 00169 mContext = NULL; 00170 } 00171 00172 public: 00175 unsigned mService; 00176 00182 string_t mKey; 00183 00189 unsigned int mFlags; 00190 00197 time_t mExpiry; 00198 00200 uint64_t mCas; 00201 00208 MCResult mResult; 00209 00213 ReadWriteBuffer mData; 00214 00216 void * mContext; 00217 }; 00218 00219 public: 00221 const static int MAX_REQUESTS = 50; 00222 00224 MemCacheClient(); 00225 00230 ~MemCacheClient(); 00231 00238 void SetTimeout(size_t aTimeoutMs); 00239 00245 void SetRetryPeriod(size_t aRetryMs); 00246 00248 static const char * ConvertResult(MCResult aResult); 00249 00251 void DumpTables(); 00252 00253 /*-----------------------------------------------------------------------*/ 00272 bool AddServer( 00273 const char * aServerAddress, 00274 const char * aServerName = NULL, 00275 unsigned aServices = (unsigned)-1); 00276 00286 bool DelServer(const char * aServerAddress); 00287 00292 void GetServers(std::vector<string_t> & aServers); 00293 00295 void ClearServers(); 00296 00297 /*-----------------------------------------------------------------------*/ 00308 inline int Add(MemRequest & aItem) { return Store("add", &aItem, 1); } 00309 00318 inline int Set(MemRequest & aItem) { return Store("set", &aItem, 1); } 00319 00327 inline int Replace(MemRequest & aItem) { return Store("replace", &aItem, 1); } 00328 00334 inline int Append(MemRequest & aItem) { return Store("append", &aItem, 1); } 00335 00341 inline int Prepend(MemRequest & aItem) { return Store("prepend", &aItem, 1); } 00342 00352 inline int CheckSet(MemRequest & aItem) { return Store("cas", &aItem, 1); } 00353 00359 inline int Get(MemRequest & aItem) { return Combine("get", &aItem, 1); } 00360 00366 inline int Gets(MemRequest & aItem) { return Combine("gets", &aItem, 1); } 00367 00373 inline int Del(MemRequest & aItem) { return Combine("del", &aItem, 1); } 00374 00375 00389 inline MCResult Increment(const char * aKey, uint64_t * aNewValue = NULL, uint64_t aDiff = 1, bool aWantReply = true, unsigned aService = 0) { 00390 return IncDec("incr", aService, aKey, aNewValue, aDiff, aWantReply); 00391 } 00392 00406 inline MCResult Decrement(const char * aKey, uint64_t * aNewValue = NULL, uint64_t aDiff = 1, bool aWantReply = true, unsigned aService = 0) { 00407 return IncDec("decr", aService, aKey, aNewValue, aDiff, aWantReply); 00408 } 00409 00410 /*-----------------------------------------------------------------------*/ 00425 inline int Add(MemRequest * aItem, int aCount) { return Store("add", aItem, aCount); } 00426 00438 inline int Set(MemRequest * aItem, int aCount) { return Store("set", aItem, aCount); } 00439 00451 inline int Replace(MemRequest * aItem, int aCount) { return Store("replace", aItem, aCount); } 00452 00462 inline int Append(MemRequest * aItem, int aCount) { return Store("append", aItem, aCount); } 00463 00473 inline int Prepend(MemRequest * aItem, int aCount) { return Store("prepend", aItem, aCount); } 00474 00488 inline int CheckSet(MemRequest * aItem, int aCount) { return Store("cas", aItem, aCount); } 00489 00499 inline int Get(MemRequest * aItem, int aCount) { return Combine("get", aItem, aCount); } 00500 00510 inline int Gets(MemRequest * aItem, int aCount) { return Combine("gets", aItem, aCount); } 00511 00521 inline int Del(MemRequest * aItem, int aCount) { return Combine("del", aItem, aCount); } 00522 00534 int FlushAll(const char * aServer = NULL, int aExpiry = 0); 00535 00538 private: 00539 // disabled 00540 MemCacheClient(const MemCacheClient &); 00541 MemCacheClient & operator=(const MemCacheClient &); 00542 00543 /*-----------------------------------------------------------------------*/ 00548 ClTrace mTrace; 00549 00550 std::vector<Server*> mServer; 00551 00552 size_t mTimeoutMs; 00553 00554 size_t mRetryMs; 00555 00562 struct ConsistentHash { 00563 unsigned long mHash; 00564 Server * mServer; 00565 unsigned mServices; 00566 int mEntry; 00567 00569 ConsistentHash(unsigned long aHash, Server * aServer, unsigned aServices, int aEntry) 00570 : mHash(aHash), mServer(aServer), mServices(aServices), mEntry(aEntry) { } 00572 ConsistentHash(const ConsistentHash & rhs) { operator=(rhs); } 00574 ConsistentHash & operator=(const ConsistentHash & rhs) { 00575 mHash = rhs.mHash; mServer = rhs.mServer; mServices = rhs.mServices; mEntry = rhs.mEntry; 00576 return *this; 00577 } 00579 inline bool services(unsigned aService) { return (mServices & aService) == aService; } 00581 bool operator<(const ConsistentHash & rhs) const; 00583 struct MatchServer; 00584 private: 00585 bool operator==(const ConsistentHash & rhs) const; 00586 }; 00587 00592 std::vector<ConsistentHash> mServerHash; 00593 00595 unsigned long CreateKeyHash(const char * aKey); 00596 00598 Server * FindServer(const string_t & aKey, unsigned aService); 00599 00601 int Store(const char * aType, MemRequest * aItem, int aCount); 00602 00604 void HandleStoreResponse(Server * aServer, MemRequest & aItem); 00605 00607 int Combine(const char * aType, MemRequest * aItem, int aCount); 00608 00610 int HandleGetResponse(Server * aServer, MemRequest ** aBegin, MemRequest ** aEnd); 00611 00613 int HandleDelResponse(Server * aServer, MemRequest ** aBegin, MemRequest ** aEnd); 00614 00616 MCResult IncDec(const char * aType, unsigned aService, const char * aKey, uint64_t * aNewValue, uint64_t aDiff, bool aWantReply); 00617 00619 }; 00620 00621 #ifdef CROSSBASE_API 00622 END_CL_NAMESPACE 00623 #endif 00624 00625 #endif // INCLUDED_MemCacheClient
1.7.3