cache.hxx

来自「vovida的软交换」· HXX 代码 · 共 247 行

HXX
247
字号
/* ==================================================================== * The Vovida Software License, Version 1.0  *  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved. *  * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: *  * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. *  * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in *    the documentation and/or other materials provided with the *    distribution. *  * 3. The names "VOCAL", "Vovida Open Communication Application Library", *    and "Vovida Open Communication Application Library (VOCAL)" must *    not be used to endorse or promote products derived from this *    software without prior written permission. For written *    permission, please contact vocal@vovida.org. * * 4. Products derived from this software may not be called "VOCAL", nor *    may "VOCAL" appear in their name, without prior written *    permission of Vovida Networks, Inc. *  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. *  * ==================================================================== *  * This software consists of voluntary contributions made by Vovida * Networks, Inc. and many individuals on behalf of Vovida Networks, * Inc.  For more information on Vovida Networks, Inc., please see * <http://www.vovida.org/>. * */ #ifndef CACHE_HXX#define CACHE_HXX/** * Cache.hxx -- 11/03/03 *  * This provides a cache system to the clients, the cache deals with  * the issues of finding the database etc, etc. */#include "RecordTypes.h"#include "ConnectionManager.hxx"#include "DBConnection.hxx"#include "DBConnThread.hxx"#include "DBInfo.h"#include "Record.hxx"#include "CacheManager.hxx"#include "CacheListener.hxx"#include <ctime>#include <map>typedef map<string, CacheListener*> listenersList;class Cache {public:  /**   * Returns the existing instance of the Cache object    * Or creates one if none exist   * A basic implementation of the singleton design    * pattern.   * @return Cache* A pointer to the single instance of the Cache   */  static Cache* getInstance(void);       /**   * Destorys the current Cache instance   */  static void destroy(void);  /**   * Signals that a given instance is now free (does nothing but update   * the usage counter)   */  static void freeInstance(void);  /*   * Database query functions   */  /**   * Fetches the specified record type using the key from the cache   * or the database, if useCache is supplied and false then the cache    * is bypassed and the database is accessed directly.   * @param rtype The type of record to be fetched   * @param key The key to use to fetch this record   * @param useCache If false bypass the cache and always access the database   * @return Record* A pointer to a copy of the record, NULL on failure.   *         the calling process is responsible for deleting this copy   */  Record* getRecord(RecordType rtype, KeyType key) {    return getRecord(rtype,key,true);  };  Record* getRecord(RecordType rtype, KeyType key, bool useCache);     /*   * Database modify functions   */  /**   * Inserts a record into the database (based on the information    * contained in the supplied record object).   * @param rec Pointer to a record to be inserted (a copy of the record   *            is inserted)   * @return bool True if insert worked, otherwise false   */  bool insertRecord(Record* rec);  /**   * Change a record in the database (based on the information contained in    * the supplied record object).   * @param rec Pointer to the record to be changed   * @return bool True if change worked, otherwise false (i.e. record not in    *         database.)   */  bool changeRecord(Record* rec);  /*   * Deletes a record from the database (based on the information   * contained in the supplied record object).   * @param rec Pointer to a record to be deleted    * @return bool True if insert worked, otherwise false   */  bool deleteRecord(Record* rec);  /*   * Update registration and de-registration   */  /**   * Registers the client for updates. The cache will listen for update    * messages from the daitabase and if recieved will first remove any   * changed data from the cache and then inform the client via the    * callback function, if supplied.   * @param hostname  Name of host for updates to be sent to   * @param rec       Type of record to recieve updates for   * @param call_back Pointer to a call back function   * @return bool True if register suceeds, otherwise false   */  // bool registerForUpdates(RecordType rec);  bool registerForUpdates(string hostname, RecordType rec, callBackFuncPtr call_back);  /**   * Clients can de-register as well   * @param hostname Name of registered host   * @param rec Type of record registered to listen for   * @return bool True is deregister suceeds, otherwise false   */  bool deregisterForUpdates(string hostname, RecordType rec);  /*   * Inital cache startup   */  /**   * This function orders the cache to load all the relavent information    * from the database into the cache. ( It uses the GetAllKeys function    * below and then performs a getRecord on each key. )   * @param rec Type of record to load in the cache   * @return bool True if it suceeds, false otherwise   */  bool initCache(RecordType rec);  /**    * This function returns a vector of all the keys   * for the Record type recType.    * @param recType Type of record to get keys for    * @return KeyList A vector of all the keys the match current database    *                 entries for that record.   */  KeyList getAllKeys(RecordType recType);  /**    * For debug purposes, displays the cache contents   */  void displayCache() {     if (myCache != NULL) { myCache->display(); }  };  /**    * Called on shutdown or after something goes wrong, effectively    * does a 'deregisterForUpdates' for each update in the system.   * This function does it's very best to clear up the database and    * destroy any listener threads.    */  void stopAllListeners();  /**    * Returns the list of aliases for the username specified by user   */  KeyList getAliasesForUser(string user);private:  // Instance of this class itself   static Cache* myInstance;      // Number of instances handed out so far  static int usageCount;  DBInfo dbinfo;  // Instance of a database manager to provide the required connections  ConnectionManager* myDBManager;  DBConnThread* myDBThread;  // Instance of the Cache manager  CacheManager* myCacheManager;  // The nested hashmap for the cache itself (defined in CacheTypes.h)  CacheStorage* myCache;  // We store the listeners locally so we can stop them all if something goes wrong  listenersList listeners;  Cache();  // Private constructor, used by getInstance()  Cache(string dbType,	string dbName, 	string dbHost, 	string dbUser, 	string dbPasswd);  ~Cache(); // Private destructor, also used by getInstance()  };#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?