程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> AMPS:Cache模塊源碼解讀

AMPS:Cache模塊源碼解讀

編輯:C++入門知識

  這裡講的Cache並不是通常說的計算機存儲系統中的高速緩存,而是軟件層面的緩存,它的做用主要是為了提高數據處理的效率。在AMPS中,Cache的結構如下:     [cpp]   /*Cache結構*/   struct _AMPSCache   {       void*   pvHASHTable;  /*Hash表,用於查找*/       void*   pvHeap;       /*堆,用於增刪改*/       int     nCacheSize;   /*Cache大小*/       /*數據處理回調函數*/       AMPS_CacheProcessUserDataCallback   pfAMPS_CacheProcessUserDataCallback;   };      /*Cache中hash和堆的結點結構*/   struct _AMPSCacheEntry   {       int             nHandle;       t_AMPSHashTableKey  oHASHTableKey;       void*           pvAMPSCache;       long            lEntryCreationTime; /*結點創建時間,做為hash的key*/       void*           pvUserData;   };     /*Cache結構*/ struct _AMPSCache { void* pvHASHTable;  /*Hash表,用於查找*/ void* pvHeap;       /*堆,用於增刪改*/ int nCacheSize;   /*Cache大小*/     /*數據處理回調函數*/ AMPS_CacheProcessUserDataCallback pfAMPS_CacheProcessUserDataCallback; };   /*Cache中hash和堆的結點結構*/ struct _AMPSCacheEntry {     int             nHandle;     t_AMPSHashTableKey oHASHTableKey; void* pvAMPSCache;     long           lEntryCreationTime; /*結點創建時間,做為hash的key*/     void*     pvUserData; };它內部維護了一個用於查找的hash表(以結點插入時間做為key值)和一個用於增刪改的堆,利用這兩個數據結構各自的優勢來實現了Cache的高速訪問。   其源碼如下:   AMPS_Cache.h       [cpp]   #ifndef __HEADER_AMPS_CACHE_H    #define __HEADER_AMPS_CACHE_H       #ifdef __cplusplus    extern "C" {   #endif       #include "AMPS_API.h"    #include "AMPS_Hash.h"    #include "AMPS_Heap.h"       typedef struct _AMPSCache        t_AMPSCache;   typedef struct _AMPSCacheEntry   t_AMPSCacheEntry;      /*Cache結構*/   struct _AMPSCache   {       void*   pvHASHTable;  /*Hash表,用於查找*/       void*   pvHeap;       /*堆,用於增刪改*/       int     nCacheSize;   /*Cache大小*/       /*數據處理回調函數*/       AMPS_CacheProcessUserDataCallback   pfAMPS_CacheProcessUserDataCallback;   };      /*Cache中hash和堆的結點結構*/   struct _AMPSCacheEntry   {       int             nHandle;       t_AMPSHashTableKey  oHASHTableKey;       void*           pvAMPSCache;       long            lEntryCreationTime; /*結點創建時間,做為hash的key*/       void*           pvUserData;   };      void* Cache_Init(void* r_pvAMPSContext, int r_nCacheSize, AMPS_CacheProcessUserDataCallback r_pfAMPS_CacheProcessUserDataCallback);   void Cache_Cleanup(void* r_pvAMPSContext, void* r_pvAMPSCache);   int Cache_FreeEntry(void* r_pvAMPSContext, void* r_pvAMPSCacheEntryToFree);      int Cache_AddEntry(void* r_pvAMPSContext, void* r_pvAMPSCache, t_AMPSHashTableKey* r_poHASHTableKey, void* r_pvData);   int Cache_RemoveEntry(void* r_pvAMPSContext, void* r_pvAMPSCache, t_AMPSHashTableKey* r_poHASHTableKey);   int Cache_UpdateEntry(void* r_pvAMPSContext, void* r_pvAMPSCache, t_AMPSHashTableKey* r_poHASHTableKey);   void* Cache_LookupEntry(void* r_pvAMPSContext, void* r_pvAMPSCache, t_AMPSHashTableKey* r_poHASHTableKey);      #ifdef __cplusplus    }   #endif       #endif //__HEADER_AMPS_CACHE_H     #ifndef __HEADER_AMPS_CACHE_H #define __HEADER_AMPS_CACHE_H   #ifdef __cplusplus extern "C" { #endif   #include "AMPS_API.h" #include "AMPS_Hash.h" #include "AMPS_Heap.h"   typedef struct _AMPSCache        t_AMPSCache; typedef struct _AMPSCacheEntry   t_AMPSCacheEntry;   /*Cache結構*/ struct _AMPSCache { void* pvHASHTable;  /*Hash表,用於查找*/ void* pvHeap;       /*堆,用於增刪改*/ int nCacheSize;   /*Cache大小*/     /*數據處理回調函數*/ AMPS_CacheProcessUserDataCallback pfAMPS_CacheProcessUserDataCallback; };   /*Cache中hash和堆的結點結構*/ struct _AMPSCacheEntry {     int             nHandle;     t_AMPSHashTableKey oHASHTableKey; void* pvAMPSCache;     long           lEntryCreationTime; /*結點創建時間,做為hash的key*/     void*     pvUserData; };   void* Cache_Init(void* r_pvAMPSContext, int r_nCacheSize, AMPS_CacheProcessUserDataCallback r_pfAMPS_CacheProcessUserDataCallback); void Cache_Cleanup(void* r_pvAMPSContext, void* r_pvAMPSCache); int Cache_FreeEntry(void* r_pvAMPSContext, void* r_pvAMPSCacheEntryToFree);   int Cache_AddEntry(void* r_pvAMPSContext, void* r_pvAMPSCache, t_AMPSHashTableKey* r_poHASHTableKey, void* r_pvData); int Cache_RemoveEntry(void* r_pvAMPSContext, void* r_pvAMPSCache, t_AMPSHashTableKey* r_poHASHTableKey); int Cache_UpdateEntry(void* r_pvAMPSContext, void* r_pvAMPSCache, t_AMPSHashTableKey* r_poHASHTableKey); void* Cache_LookupEntry(void* r_pvAMPSContext, void* r_pvAMPSCache, t_AMPSHashTableKey* r_poHASHTableKey);   #ifdef __cplusplus } #endif   #endif //__HEADER_AMPS_CACHE_H   AMPS_Cache.c       [cpp]  #include "AMPS_Core.h"    #include "AMPS_Defines.h"    #include "AMPS_MemMgt.h"    #include "AMPS_LinkList.h"    #include "AMPS_Cache.h"       /*****************************************************************  函數名稱: Cache_Init  功能描述: Cache模塊初始化  入參::        void* r_pvAMPSContext APMS應用上下文        int r_nCacheSize  Cache大小        AMPS_CacheProcessUserDataCallback r_pfAMPS_CacheProcessUserDataCallback                  出參:          返回值:        void* Cache句柄    *****************************************************************/   void* Cache_Init(void* r_pvAMPSContext, int r_nCacheSize, AMPS_CacheProcessUserDataCallback r_pfAMPS_CacheProcessUserDataCallback)   {       t_AMPSCache* poAMPSCache = NULL;          TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");          /*分配一個Cache對象*/       poAMPSCache = AMPS_InternalMalloc(sizeof(t_AMPSCache));       if (NULL == poAMPSCache)       {           TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "AMPS_InternalMalloc failed for poAMPSCache.\n");           return NULL;       }          /*指定Cache大小*/       poAMPSCache->nCacheSize = r_nCacheSize;          /*Cache中結點數據處理回調函數*/       TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Set poAMPSCache->pfAMPS_CacheProcessUserDataCallback to user given function.\n");       poAMPSCache->pfAMPS_CacheProcessUserDataCallback = r_pfAMPS_CacheProcessUserDataCallback;          /*創建Cache所使用的Hash表,用於查找*/       poAMPSCache->pvHASHTable = HashTable_Create(r_pvAMPSContext, poAMPSCache->nCacheSize, NULL);       if (NULL == poAMPSCache->pvHASHTable)       {           TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "HashTable_Create failed.\n");           return NULL;       }          /*創建Cache所使用堆,用於增、刪、改*/       poAMPSCache->pvHeap = Heap_Init(r_pvAMPSContext, poAMPSCache->nCacheSize, Cache_FreeEntry);       if(NULL == poAMPSCache->pvHeap)       {           TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "HeapInit failed.\n");           return NULL;       }          TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n");       return poAMPSCache;   }      /*****************************************************************  函數名稱: Cache_Cleanup  功能描述: Cache模塊銷毀  入參::        void* r_pvAMPSContext APMS應用上下文        void* r_pvAMPSCache Cache句柄          出參:          返回值:        void  *****************************************************************/   void Cache_Cleanup(void* r_pvAMPSContext, void* r_pvAMPSCache)   {       t_AMPSCache* poAMPSCache = r_pvAMPSCache;          TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");          /*釋放Hash表*/       TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "HashTable_Delete called.\n");       HashTable_Delete(r_pvAMPSContext, poAMPSCache->pvHASHTable);          /*釋放堆*/       TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Heap_Cleanup called.\n");       Heap_Cleanup(r_pvAMPSContext, poAMPSCache->pvHeap);          /*釋放Cache句柄*/       TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "AMPS_InternalFree called for poAMPSCache.\n");       AMPS_InternalFree(poAMPSCache);          TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n");   }      /*****************************************************************  函數名稱: Cache_AddEntry  功能描述: 向Cache內寫數  入參::        void* r_pvAMPSContext APMS應用上下文        void* r_pvAMPSCache Cache句柄        t_AMPSHashTableKey* r_poHASHTableKey Cache結點        void* r_pvData 結點數  出參:          返回值:        void  *****************************************************************/   int Cache_AddEntry(void* r_pvAMPSContext, void* r_pvAMPSCache, t_AMPSHashTableKey* r_poHASHTableKey, void* r_pvData)   {       t_AMPSCache* poAMPSCache = r_pvAMPSCache;       t_Heap* poHeap = poAMPSCache->pvHeap;       t_AMPSCacheEntry* poAMPSCacheEntryToAdd =  NULL;          TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");          /*分配一個Cache結點*/       poAMPSCacheEntryToAdd = AMPS_InternalMalloc(sizeof(t_AMPSCacheEntry));       if (NULL == poAMPSCacheEntryToAdd)       {           TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "AMPS_InternalMalloc failed for poAMPSCacheEntryToAdd.\n");           return AMPS_ERROR_FAILURE;       }              /*結點賦值*/       poAMPSCacheEntryToAdd->oHASHTableKey = *r_poHASHTableKey; /*存放hash結點*/       poAMPSCacheEntryToAdd->pvAMPSCache = poAMPSCache; /*存放Cache句柄*/       poAMPSCacheEntryToAdd->pvUserData = r_pvData; /*存放數據*/       /*結點創建時間,這個時間是cache中hash的key值*/       poAMPSCacheEntryToAdd->lEntryCreationTime = SAPI_GetCurrentTimeInMilliSec(r_pvAMPSContext)/1000; //Set creation time           /*Cache已滿*/       if(poHeap->nIndex >= poAMPSCache->nCacheSize) //Cache is full, remove Entry from Cache        {           t_HeapNode* poHeapNodeLeastRefered = NULL;              TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Cache is full, Remove Entry from Cache.\n");              /*獲取最小堆根結點,即進入Cache最早的結點*/           if(AMPS_SUCCESS != Heap_GetMinNode(r_pvAMPSContext, poHeap, (void**)&poHeapNodeLeastRefered))           {               TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "HeapGetMinNode failed.\n");               AMPS_InternalFree(poAMPSCacheEntryToAdd);               return AMPS_ERROR_FAILURE;           }              /*從堆中釋放*/           if(AMPS_SUCCESS != Cache_RemoveEntry(r_pvAMPSContext, r_pvAMPSCache, poHeapNodeLeastRefered->pvDataValue))           {               TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "Cache_RemoveEntry failed.\n");               AMPS_InternalFree(poAMPSCacheEntryToAdd);               return AMPS_ERROR_FAILURE;           }       }          /*將hash結點寫入*/       poAMPSCacheEntryToAdd->nHandle = HashTable_Insert(r_pvAMPSContext, poAMPSCache->pvHASHTable, poAMPSCacheEntryToAdd, &poAMPSCacheEntryToAdd->oHASHTableKey);       if(AMPS_INVALID_HANDLE == poAMPSCacheEntryToAdd->nHandle)       {           TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "HashTable_Insert failed.\n");                    AMPS_InternalFree(poAMPSCacheEntryToAdd);           return AMPS_ERROR_FAILURE;       }          /*將新結點寫入堆*/       TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Inserting new Entry into Heap.\n");       if(AMPS_ERROR_FAILURE == Heap_InsertMin(r_pvAMPSContext, poHeap, poAMPSCacheEntryToAdd, poAMPSCacheEntryToAdd->lEntryCreationTime))       {           TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "HeapInsertMin failed.\n");           AMPS_InternalFree(poAMPSCacheEntryToAdd);           return AMPS_ERROR_FAILURE;       }          TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n");       return AMPS_SUCCESS;   }      /*****************************************************************  函數名稱: Cache_RemoveEntry  功能描述: 從Cache中刪除數據  入參::        void* r_pvAMPSContext APMS應用上下文        void* r_pvAMPSCache Cache句柄        t_AMPSHashTableKey* r_poHASHTableKey Cache結點  出參:          返回值:        int  *****************************************************************/   int Cache_RemoveEntry(void* r_pvAMPSContext, void* r_pvAMPSCache, t_AMPSHashTableKey* r_poHASHTableKey)   {       t_AMPSCache* poAMPSCache = r_pvAMPSCache;       t_Heap* poHeap = poAMPSCache->pvHeap;       t_AMPSCacheEntry* poAMPSCacheEntryToRemove =  NULL;          TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");          /*查找cache結點*/       poAMPSCacheEntryToRemove = HashTable_Search(r_pvAMPSContext, poAMPSCache->pvHASHTable, r_poHASHTableKey);       if(NULL != poAMPSCacheEntryToRemove)       {           /*從hash表中刪除*/           TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Removing Entry from HashTable.\n");           if(AMPS_SUCCESS != HashTable_RemoveByHandle(r_pvAMPSContext, poAMPSCache->pvHASHTable, poAMPSCacheEntryToRemove->nHandle))           {               TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "HashTable_RemoveByHandle failed.\n");               return AMPS_ERROR_FAILURE;           }                 /*從堆中刪除*/           TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Removing Entry from Heap.\n");           if (AMPS_SUCCESS != Heap_RemoveNodeMin(r_pvAMPSContext, poHeap, poAMPSCacheEntryToRemove->lEntryCreationTime))           {               TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "Heap_RemoveNodeMin failed.\n");               return AMPS_ERROR_FAILURE;           }       }          TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n");       return AMPS_SUCCESS;   }      /*****************************************************************  函數名稱: Cache_UpdateEntry  功能描述: 更新Cache中的數據  入參::        void* r_pvAMPSContext APMS應用上下文        void* r_pvAMPSCache Cache句柄        t_AMPSHashTableKey* r_poHASHTableKey Cache結點  出參:          返回值:        int  *****************************************************************/   int Cache_UpdateEntry(void* r_pvAMPSContext, void* r_pvAMPSCache, t_AMPSHashTableKey* r_poHASHTableKey)   {       t_AMPSCache* poAMPSCache = r_pvAMPSCache;       t_Heap* poHeap = poAMPSCache->pvHeap;       t_AMPSCacheEntry* poAMPSCacheEntryToUpdate =  NULL;       long lOldEntryCreationTime = 0;          TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");          /*查找*/       poAMPSCacheEntryToUpdate = HashTable_Search(r_pvAMPSContext, poAMPSCache->pvHASHTable, r_poHASHTableKey);       if(NULL != poAMPSCacheEntryToUpdate)       {           /*以時間為key值*/           lOldEntryCreationTime = poAMPSCacheEntryToUpdate->lEntryCreationTime;           poAMPSCacheEntryToUpdate->lEntryCreationTime = SAPI_GetCurrentTimeInMilliSec(r_pvAMPSContext)/1000;              /*更新*/           if(AMPS_SUCCESS != Heap_UpdateNodeMinKey(r_pvAMPSContext, poHeap, lOldEntryCreationTime, poAMPSCacheEntryToUpdate->lEntryCreationTime))           {               TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "Heap_UpdateNodeMinKey failed.\n");               return AMPS_ERROR_FAILURE;           }       }       else       {           TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "HashTable_Search failed.\n");           return AMPS_ERROR_FAILURE;       }          TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n");       return AMPS_SUCCESS;   }      /*****************************************************************  函數名稱: Cache_LookupEntry  功能描述: 查找Cache中的數據  入參::        void* r_pvAMPSContext APMS應用上下文        void* r_pvAMPSCache Cache句柄        t_AMPSHashTableKey* r_poHASHTableKey Cache結點  出參:          返回值:        void* 查找到的結點  *****************************************************************/   void* Cache_LookupEntry(void* r_pvAMPSContext, void* r_pvAMPSCache, t_AMPSHashTableKey* r_poHASHTableKey)   {       t_AMPSCache* poAMPSCache = r_pvAMPSCache;       t_Heap* poHeap = poAMPSCache->pvHeap;       t_AMPSCacheEntry* poAMPSCacheEntryToFind =  NULL;       long lOldEntryCreationTime = 0;          TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");          poAMPSCacheEntryToFind = HashTable_Search(r_pvAMPSContext, poAMPSCache->pvHASHTable, r_poHASHTableKey);       if(NULL != poAMPSCacheEntryToFind)       {           lOldEntryCreationTime = poAMPSCacheEntryToFind->lEntryCreationTime;           poAMPSCacheEntryToFind->lEntryCreationTime = SAPI_GetCurrentTimeInMilliSec(r_pvAMPSContext)/1000;              /*找到後,更新一下時間*/           if(AMPS_SUCCESS != Heap_UpdateNodeMinKey(r_pvAMPSContext, poHeap, lOldEntryCreationTime, poAMPSCacheEntryToFind->lEntryCreationTime))           {               TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "Heap_UpdateNodeMinKey failed.\n");               return NULL;           }       }       else       {           TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "HashTable_Search failed.\n");           return NULL;       }          TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n");       return(poAMPSCacheEntryToFind->pvUserData);   }      /*****************************************************************  函數名稱: Cache_FreeEntry  功能描述: 釋放Cache結點  入參::        void* r_pvAMPSContext APMS應用上下文        void* r_pvAMPSCacheEntryToFree Cache結點  出參:          返回值:        int  *****************************************************************/   int Cache_FreeEntry(void* r_pvAMPSContext, void* r_pvAMPSCacheEntryToFree)   {       t_AMPSCacheEntry* poAMPSCacheEntryToFree =  r_pvAMPSCacheEntryToFree;       t_AMPSCache* poAMPSCache = poAMPSCacheEntryToFree->pvAMPSCache;          TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");          /*釋放前可能過注冊的回調函數做處理*/       if(NULL != poAMPSCache->pfAMPS_CacheProcessUserDataCallback)       {           TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "poAMPSCache->pfAMPS_CacheProcessUserDataCallback called.\n");           poAMPSCache->pfAMPS_CacheProcessUserDataCallback(r_pvAMPSContext, poAMPSCacheEntryToFree->pvUserData);       }          TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "AMPS_InternalFree called for poAMPSCacheEntryToRemove.\n");       AMPS_InternalFree(poAMPSCacheEntryToFree);          TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n");       return AMPS_SUCCESS;   }     #include "AMPS_Core.h" #include "AMPS_Defines.h" #include "AMPS_MemMgt.h" #include "AMPS_LinkList.h" #include "AMPS_Cache.h"   /***************************************************************** 函數名稱: Cache_Init 功能描述: Cache模塊初始化 入參::       void* r_pvAMPSContext APMS應用上下文       int r_nCacheSize  Cache大小       AMPS_CacheProcessUserDataCallback r_pfAMPS_CacheProcessUserDataCallback               出參:        返回值:       void* Cache句柄   *****************************************************************/ void* Cache_Init(void* r_pvAMPSContext, int r_nCacheSize, AMPS_CacheProcessUserDataCallback r_pfAMPS_CacheProcessUserDataCallback) { t_AMPSCache* poAMPSCache = NULL;       TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");       /*分配一個Cache對象*/     poAMPSCache = AMPS_InternalMalloc(sizeof(t_AMPSCache));     if (NULL == poAMPSCache)     {         TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "AMPS_InternalMalloc failed for poAMPSCache.\n");         return NULL;     }       /*指定Cache大小*/ poAMPSCache->nCacheSize = r_nCacheSize;       /*Cache中結點數據處理回調函數*/ TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Set poAMPSCache->pfAMPS_CacheProcessUserDataCallback to user given function.\n"); poAMPSCache->pfAMPS_CacheProcessUserDataCallback = r_pfAMPS_CacheProcessUserDataCallback;       /*創建Cache所使用的Hash表,用於查找*/     poAMPSCache->pvHASHTable = HashTable_Create(r_pvAMPSContext, poAMPSCache->nCacheSize, NULL);     if (NULL == poAMPSCache->pvHASHTable)     {         TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "HashTable_Create failed.\n");         return NULL;     }       /*創建Cache所使用堆,用於增、刪、改*/     poAMPSCache->pvHeap = Heap_Init(r_pvAMPSContext, poAMPSCache->nCacheSize, Cache_FreeEntry);     if(NULL == poAMPSCache->pvHeap)     {         TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "HeapInit failed.\n");         return NULL;     }   TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n"); return poAMPSCache; }   /***************************************************************** 函數名稱: Cache_Cleanup 功能描述: Cache模塊銷毀 入參::       void* r_pvAMPSContext APMS應用上下文       void* r_pvAMPSCache Cache句柄        出參:        返回值:       void *****************************************************************/ void Cache_Cleanup(void* r_pvAMPSContext, void* r_pvAMPSCache) { t_AMPSCache* poAMPSCache = r_pvAMPSCache;       TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");       /*釋放Hash表*/     TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "HashTable_Delete called.\n");     HashTable_Delete(r_pvAMPSContext, poAMPSCache->pvHASHTable);       /*釋放堆*/     TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Heap_Cleanup called.\n");     Heap_Cleanup(r_pvAMPSContext, poAMPSCache->pvHeap);       /*釋放Cache句柄*/     TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "AMPS_InternalFree called for poAMPSCache.\n");     AMPS_InternalFree(poAMPSCache);       TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n"); }   /***************************************************************** 函數名稱: Cache_AddEntry 功能描述: 向Cache內寫數 入參::       void* r_pvAMPSContext APMS應用上下文       void* r_pvAMPSCache Cache句柄       t_AMPSHashTableKey* r_poHASHTableKey Cache結點       void* r_pvData 結點數 出參:        返回值:       void *****************************************************************/ int Cache_AddEntry(void* r_pvAMPSContext, void* r_pvAMPSCache, t_AMPSHashTableKey* r_poHASHTableKey, void* r_pvData) { t_AMPSCache* poAMPSCache = r_pvAMPSCache;     t_Heap* poHeap = poAMPSCache->pvHeap; t_AMPSCacheEntry* poAMPSCacheEntryToAdd =  NULL;   TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");       /*分配一個Cache結點*/ poAMPSCacheEntryToAdd = AMPS_InternalMalloc(sizeof(t_AMPSCacheEntry));     if (NULL == poAMPSCacheEntryToAdd)     {         TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "AMPS_InternalMalloc failed for poAMPSCacheEntryToAdd.\n");         return AMPS_ERROR_FAILURE;     }          /*結點賦值*/ poAMPSCacheEntryToAdd->oHASHTableKey = *r_poHASHTableKey; /*存放hash結點*/ poAMPSCacheEntryToAdd->pvAMPSCache = poAMPSCache; /*存放Cache句柄*/ poAMPSCacheEntryToAdd->pvUserData = r_pvData; /*存放數據*/     /*結點創建時間,這個時間是cache中hash的key值*/     poAMPSCacheEntryToAdd->lEntryCreationTime = SAPI_GetCurrentTimeInMilliSec(r_pvAMPSContext)/1000; //Set creation time       /*Cache已滿*/ if(poHeap->nIndex >= poAMPSCache->nCacheSize) //Cache is full, remove Entry from Cache     {         t_HeapNode* poHeapNodeLeastRefered = NULL;           TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Cache is full, Remove Entry from Cache.\n");           /*獲取最小堆根結點,即進入Cache最早的結點*/         if(AMPS_SUCCESS != Heap_GetMinNode(r_pvAMPSContext, poHeap, (void**)&poHeapNodeLeastRefered))         {             TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "HeapGetMinNode failed.\n"); AMPS_InternalFree(poAMPSCacheEntryToAdd);             return AMPS_ERROR_FAILURE;         }           /*從堆中釋放*/ if(AMPS_SUCCESS != Cache_RemoveEntry(r_pvAMPSContext, r_pvAMPSCache, poHeapNodeLeastRefered->pvDataValue)) {             TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "Cache_RemoveEntry failed.\n"); AMPS_InternalFree(poAMPSCacheEntryToAdd);             return AMPS_ERROR_FAILURE; }     }       /*將hash結點寫入*/ poAMPSCacheEntryToAdd->nHandle = HashTable_Insert(r_pvAMPSContext, poAMPSCache->pvHASHTable, poAMPSCacheEntryToAdd, &poAMPSCacheEntryToAdd->oHASHTableKey);     if(AMPS_INVALID_HANDLE == poAMPSCacheEntryToAdd->nHandle)     {         TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "HashTable_Insert failed.\n");          AMPS_InternalFree(poAMPSCacheEntryToAdd);         return AMPS_ERROR_FAILURE;     }       /*將新結點寫入堆*/     TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Inserting new Entry into Heap.\n"); if(AMPS_ERROR_FAILURE == Heap_InsertMin(r_pvAMPSContext, poHeap, poAMPSCacheEntryToAdd, poAMPSCacheEntryToAdd->lEntryCreationTime))     {         TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "HeapInsertMin failed.\n"); AMPS_InternalFree(poAMPSCacheEntryToAdd);         return AMPS_ERROR_FAILURE;     }       TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n"); return AMPS_SUCCESS; }   /***************************************************************** 函數名稱: Cache_RemoveEntry 功能描述: 從Cache中刪除數據 入參::       void* r_pvAMPSContext APMS應用上下文       void* r_pvAMPSCache Cache句柄       t_AMPSHashTableKey* r_poHASHTableKey Cache結點 出參:        返回值:       int *****************************************************************/ int Cache_RemoveEntry(void* r_pvAMPSContext, void* r_pvAMPSCache, t_AMPSHashTableKey* r_poHASHTableKey) { t_AMPSCache* poAMPSCache = r_pvAMPSCache;     t_Heap* poHeap = poAMPSCache->pvHeap; t_AMPSCacheEntry* poAMPSCacheEntryToRemove =  NULL;   TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");       /*查找cache結點*/ poAMPSCacheEntryToRemove = HashTable_Search(r_pvAMPSContext, poAMPSCache->pvHASHTable, r_poHASHTableKey); if(NULL != poAMPSCacheEntryToRemove) {         /*從hash表中刪除*/ TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Removing Entry from HashTable.\n"); if(AMPS_SUCCESS != HashTable_RemoveByHandle(r_pvAMPSContext, poAMPSCache->pvHASHTable, poAMPSCacheEntryToRemove->nHandle)) { TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "HashTable_RemoveByHandle failed.\n"); return AMPS_ERROR_FAILURE; }             /*從堆中刪除*/ TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Removing Entry from Heap.\n"); if (AMPS_SUCCESS != Heap_RemoveNodeMin(r_pvAMPSContext, poHeap, poAMPSCacheEntryToRemove->lEntryCreationTime)) { TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "Heap_RemoveNodeMin failed.\n"); return AMPS_ERROR_FAILURE; } }   TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n"); return AMPS_SUCCESS; }   /***************************************************************** 函數名稱: Cache_UpdateEntry 功能描述: 更新Cache中的數據 入參::       void* r_pvAMPSContext APMS應用上下文       void* r_pvAMPSCache Cache句柄       t_AMPSHashTableKey* r_poHASHTableKey Cache結點 出參:        返回值:       int *****************************************************************/ int Cache_UpdateEntry(void* r_pvAMPSContext, void* r_pvAMPSCache, t_AMPSHashTableKey* r_poHASHTableKey) { t_AMPSCache* poAMPSCache = r_pvAMPSCache;     t_Heap* poHeap = poAMPSCache->pvHeap; t_AMPSCacheEntry* poAMPSCacheEntryToUpdate =  NULL;     long lOldEntryCreationTime = 0;   TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");       /*查找*/ poAMPSCacheEntryToUpdate = HashTable_Search(r_pvAMPSContext, poAMPSCache->pvHASHTable, r_poHASHTableKey); if(NULL != poAMPSCacheEntryToUpdate) {         /*以時間為key值*/ lOldEntryCreationTime = poAMPSCacheEntryToUpdate->lEntryCreationTime; poAMPSCacheEntryToUpdate->lEntryCreationTime = SAPI_GetCurrentTimeInMilliSec(r_pvAMPSContext)/1000;           /*更新*/ if(AMPS_SUCCESS != Heap_UpdateNodeMinKey(r_pvAMPSContext, poHeap, lOldEntryCreationTime, poAMPSCacheEntryToUpdate->lEntryCreationTime)) { TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "Heap_UpdateNodeMinKey failed.\n"); return AMPS_ERROR_FAILURE; } } else { TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "HashTable_Search failed.\n"); return AMPS_ERROR_FAILURE; }   TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n"); return AMPS_SUCCESS; }   /***************************************************************** 函數名稱: Cache_LookupEntry 功能描述: 查找Cache中的數據 入參::       void* r_pvAMPSContext APMS應用上下文       void* r_pvAMPSCache Cache句柄       t_AMPSHashTableKey* r_poHASHTableKey Cache結點 出參:        返回值:       void* 查找到的結點 *****************************************************************/ void* Cache_LookupEntry(void* r_pvAMPSContext, void* r_pvAMPSCache, t_AMPSHashTableKey* r_poHASHTableKey) { t_AMPSCache* poAMPSCache = r_pvAMPSCache;     t_Heap* poHeap = poAMPSCache->pvHeap; t_AMPSCacheEntry* poAMPSCacheEntryToFind =  NULL;     long lOldEntryCreationTime = 0;       TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");   poAMPSCacheEntryToFind = HashTable_Search(r_pvAMPSContext, poAMPSCache->pvHASHTable, r_poHASHTableKey); if(NULL != poAMPSCacheEntryToFind) { lOldEntryCreationTime = poAMPSCacheEntryToFind->lEntryCreationTime; poAMPSCacheEntryToFind->lEntryCreationTime = SAPI_GetCurrentTimeInMilliSec(r_pvAMPSContext)/1000;           /*找到後,更新一下時間*/ if(AMPS_SUCCESS != Heap_UpdateNodeMinKey(r_pvAMPSContext, poHeap, lOldEntryCreationTime, poAMPSCacheEntryToFind->lEntryCreationTime)) { TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "Heap_UpdateNodeMinKey failed.\n"); return NULL; } } else { TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "HashTable_Search failed.\n"); return NULL; }   TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n"); return(poAMPSCacheEntryToFind->pvUserData); }   /***************************************************************** 函數名稱: Cache_FreeEntry 功能描述: 釋放Cache結點 入參::       void* r_pvAMPSContext APMS應用上下文       void* r_pvAMPSCacheEntryToFree Cache結點 出參:        返回值:       int *****************************************************************/ int Cache_FreeEntry(void* r_pvAMPSContext, void* r_pvAMPSCacheEntryToFree) { t_AMPSCacheEntry* poAMPSCacheEntryToFree =  r_pvAMPSCacheEntryToFree; t_AMPSCache* poAMPSCache = poAMPSCacheEntryToFree->pvAMPSCache;   TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");       /*釋放前可能過注冊的回調函數做處理*/ if(NULL != poAMPSCache->pfAMPS_CacheProcessUserDataCallback) { TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "poAMPSCache->pfAMPS_CacheProcessUserDataCallback called.\n"); poAMPSCache->pfAMPS_CacheProcessUserDataCallback(r_pvAMPSContext, poAMPSCacheEntryToFree->pvUserData); }   TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "AMPS_InternalFree called for poAMPSCacheEntryToRemove.\n"); AMPS_InternalFree(poAMPSCacheEntryToFree);   TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n"); return AMPS_SUCCESS; }       這裡講的Cache並不是通常說的計算機存儲系統中的高速緩存,而是軟件層面的緩存,它的做用主要是為了提高數據處理的效率。在AMPS中,Cache的結構如下:     [cpp]   /*Cache結構*/   struct _AMPSCache   {       void*   pvHASHTable;  /*Hash表,用於查找*/       void*   pvHeap;       /*堆,用於增刪改*/       int     nCacheSize;   /*Cache大小*/       /*數據處理回調函數*/       AMPS_CacheProcessUserDataCallback   pfAMPS_CacheProcessUserDataCallback;   };      /*Cache中hash和堆的結點結構*/   struct _AMPSCacheEntry   {       int             nHandle;       t_AMPSHashTableKey  oHASHTableKey;       void*           pvAMPSCache;       long            lEntryCreationTime; /*結點創建時間,做為hash的key*/       void*           pvUserData;   };     /*Cache結構*/ struct _AMPSCache { void* pvHASHTable;  /*Hash表,用於查找*/ void* pvHeap;       /*堆,用於增刪改*/ int nCacheSize;   /*Cache大小*/     /*數據處理回調函數*/ AMPS_CacheProcessUserDataCallback pfAMPS_CacheProcessUserDataCallback; };   /*Cache中hash和堆的結點結構*/ struct _AMPSCacheEntry {     int             nHandle;     t_AMPSHashTableKey oHASHTableKey; void* pvAMPSCache;     long           lEntryCreationTime; /*結點創建時間,做為hash的key*/     void*     pvUserData; };它內部維護了一個用於查找的hash表(以結點插入時間做為key值)和一個用於增刪改的堆,利用這兩個數據結構各自的優勢來實現了Cache的高速訪問。   其源碼如下:   AMPS_Cache.h       [cpp]   #ifndef __HEADER_AMPS_CACHE_H    #define __HEADER_AMPS_CACHE_H       #ifdef __cplusplus    extern "C" {   #endif       #include "AMPS_API.h"    #include "AMPS_Hash.h"    #include "AMPS_Heap.h"       typedef struct _AMPSCache        t_AMPSCache;   typedef struct _AMPSCacheEntry   t_AMPSCacheEntry;      /*Cache結構*/   struct _AMPSCache   {       void*   pvHASHTable;  /*Hash表,用於查找*/       void*   pvHeap;       /*堆,用於增刪改*/       int     nCacheSize;   /*Cache大小*/       /*數據處理回調函數*/       AMPS_CacheProcessUserDataCallback   pfAMPS_CacheProcessUserDataCallback;   };      /*Cache中hash和堆的結點結構*/   struct _AMPSCacheEntry   {       int             nHandle;       t_AMPSHashTableKey  oHASHTableKey;       void*           pvAMPSCache;       long            lEntryCreationTime; /*結點創建時間,做為hash的key*/       void*           pvUserData;   };      void* Cache_Init(void* r_pvAMPSContext, int r_nCacheSize, AMPS_CacheProcessUserDataCallback r_pfAMPS_CacheProcessUserDataCallback);   void Cache_Cleanup(void* r_pvAMPSContext, void* r_pvAMPSCache);   int Cache_FreeEntry(void* r_pvAMPSContext, void* r_pvAMPSCacheEntryToFree);      int Cache_AddEntry(void* r_pvAMPSContext, void* r_pvAMPSCache, t_AMPSHashTableKey* r_poHASHTableKey, void* r_pvData);   int Cache_RemoveEntry(void* r_pvAMPSContext, void* r_pvAMPSCache, t_AMPSHashTableKey* r_poHASHTableKey);   int Cache_UpdateEntry(void* r_pvAMPSContext, void* r_pvAMPSCache, t_AMPSHashTableKey* r_poHASHTableKey);   void* Cache_LookupEntry(void* r_pvAMPSContext, void* r_pvAMPSCache, t_AMPSHashTableKey* r_poHASHTableKey);      #ifdef __cplusplus    }   #endif       #endif //__HEADER_AMPS_CACHE_H     #ifndef __HEADER_AMPS_CACHE_H #define __HEADER_AMPS_CACHE_H   #ifdef __cplusplus extern "C" { #endif   #include "AMPS_API.h" #include "AMPS_Hash.h" #include "AMPS_Heap.h"   typedef struct _AMPSCache        t_AMPSCache; typedef struct _AMPSCacheEntry   t_AMPSCacheEntry;   /*Cache結構*/ struct _AMPSCache { void* pvHASHTable;  /*Hash表,用於查找*/ void* pvHeap;       /*堆,用於增刪改*/ int nCacheSize;   /*Cache大小*/     /*數據處理回調函數*/ AMPS_CacheProcessUserDataCallback pfAMPS_CacheProcessUserDataCallback; };   /*Cache中hash和堆的結點結構*/ struct _AMPSCacheEntry {     int             nHandle;     t_AMPSHashTableKey oHASHTableKey; void* pvAMPSCache;     long           lEntryCreationTime; /*結點創建時間,做為hash的key*/     void*     pvUserData; };   void* Cache_Init(void* r_pvAMPSContext, int r_nCacheSize, AMPS_CacheProcessUserDataCallback r_pfAMPS_CacheProcessUserDataCallback); void Cache_Cleanup(void* r_pvAMPSContext, void* r_pvAMPSCache); int Cache_FreeEntry(void* r_pvAMPSContext, void* r_pvAMPSCacheEntryToFree);   int Cache_AddEntry(void* r_pvAMPSContext, void* r_pvAMPSCache, t_AMPSHashTableKey* r_poHASHTableKey, void* r_pvData); int Cache_RemoveEntry(void* r_pvAMPSContext, void* r_pvAMPSCache, t_AMPSHashTableKey* r_poHASHTableKey); int Cache_UpdateEntry(void* r_pvAMPSContext, void* r_pvAMPSCache, t_AMPSHashTableKey* r_poHASHTableKey); void* Cache_LookupEntry(void* r_pvAMPSContext, void* r_pvAMPSCache, t_AMPSHashTableKey* r_poHASHTableKey);   #ifdef __cplusplus } #endif   #endif //__HEADER_AMPS_CACHE_H   AMPS_Cache.c       [cpp]  #include "AMPS_Core.h"    #include "AMPS_Defines.h"    #include "AMPS_MemMgt.h"    #include "AMPS_LinkList.h"    #include "AMPS_Cache.h"       /*****************************************************************  函數名稱: Cache_Init  功能描述: Cache模塊初始化  入參::        void* r_pvAMPSContext APMS應用上下文        int r_nCacheSize  Cache大小        AMPS_CacheProcessUserDataCallback r_pfAMPS_CacheProcessUserDataCallback                  出參:          返回值:        void* Cache句柄    *****************************************************************/   void* Cache_Init(void* r_pvAMPSContext, int r_nCacheSize, AMPS_CacheProcessUserDataCallback r_pfAMPS_CacheProcessUserDataCallback)   {       t_AMPSCache* poAMPSCache = NULL;          TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");          /*分配一個Cache對象*/       poAMPSCache = AMPS_InternalMalloc(sizeof(t_AMPSCache));       if (NULL == poAMPSCache)       {           TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "AMPS_InternalMalloc failed for poAMPSCache.\n");           return NULL;       }          /*指定Cache大小*/       poAMPSCache->nCacheSize = r_nCacheSize;          /*Cache中結點數據處理回調函數*/       TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Set poAMPSCache->pfAMPS_CacheProcessUserDataCallback to user given function.\n");       poAMPSCache->pfAMPS_CacheProcessUserDataCallback = r_pfAMPS_CacheProcessUserDataCallback;          /*創建Cache所使用的Hash表,用於查找*/       poAMPSCache->pvHASHTable = HashTable_Create(r_pvAMPSContext, poAMPSCache->nCacheSize, NULL);       if (NULL == poAMPSCache->pvHASHTable)       {           TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "HashTable_Create failed.\n");           return NULL;       }          /*創建Cache所使用堆,用於增、刪、改*/       poAMPSCache->pvHeap = Heap_Init(r_pvAMPSContext, poAMPSCache->nCacheSize, Cache_FreeEntry);       if(NULL == poAMPSCache->pvHeap)       {           TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "HeapInit failed.\n");           return NULL;       }          TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n");       return poAMPSCache;   }      /*****************************************************************  函數名稱: Cache_Cleanup  功能描述: Cache模塊銷毀  入參::        void* r_pvAMPSContext APMS應用上下文        void* r_pvAMPSCache Cache句柄          出參:          返回值:        void  *****************************************************************/   void Cache_Cleanup(void* r_pvAMPSContext, void* r_pvAMPSCache)   {       t_AMPSCache* poAMPSCache = r_pvAMPSCache;          TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");          /*釋放Hash表*/       TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "HashTable_Delete called.\n");       HashTable_Delete(r_pvAMPSContext, poAMPSCache->pvHASHTable);          /*釋放堆*/       TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Heap_Cleanup called.\n");       Heap_Cleanup(r_pvAMPSContext, poAMPSCache->pvHeap);          /*釋放Cache句柄*/       TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "AMPS_InternalFree called for poAMPSCache.\n");       AMPS_InternalFree(poAMPSCache);          TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n");   }      /*****************************************************************  函數名稱: Cache_AddEntry  功能描述: 向Cache內寫數  入參::        void* r_pvAMPSContext APMS應用上下文        void* r_pvAMPSCache Cache句柄        t_AMPSHashTableKey* r_poHASHTableKey Cache結點        void* r_pvData 結點數  出參:          返回值:        void  *****************************************************************/   int Cache_AddEntry(void* r_pvAMPSContext, void* r_pvAMPSCache, t_AMPSHashTableKey* r_poHASHTableKey, void* r_pvData)   {       t_AMPSCache* poAMPSCache = r_pvAMPSCache;       t_Heap* poHeap = poAMPSCache->pvHeap;       t_AMPSCacheEntry* poAMPSCacheEntryToAdd =  NULL;          TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");          /*分配一個Cache結點*/       poAMPSCacheEntryToAdd = AMPS_InternalMalloc(sizeof(t_AMPSCacheEntry));       if (NULL == poAMPSCacheEntryToAdd)       {           TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "AMPS_InternalMalloc failed for poAMPSCacheEntryToAdd.\n");           return AMPS_ERROR_FAILURE;       }              /*結點賦值*/       poAMPSCacheEntryToAdd->oHASHTableKey = *r_poHASHTableKey; /*存放hash結點*/       poAMPSCacheEntryToAdd->pvAMPSCache = poAMPSCache; /*存放Cache句柄*/       poAMPSCacheEntryToAdd->pvUserData = r_pvData; /*存放數據*/       /*結點創建時間,這個時間是cache中hash的key值*/       poAMPSCacheEntryToAdd->lEntryCreationTime = SAPI_GetCurrentTimeInMilliSec(r_pvAMPSContext)/1000; //Set creation time           /*Cache已滿*/       if(poHeap->nIndex >= poAMPSCache->nCacheSize) //Cache is full, remove Entry from Cache        {           t_HeapNode* poHeapNodeLeastRefered = NULL;              TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Cache is full, Remove Entry from Cache.\n");              /*獲取最小堆根結點,即進入Cache最早的結點*/           if(AMPS_SUCCESS != Heap_GetMinNode(r_pvAMPSContext, poHeap, (void**)&poHeapNodeLeastRefered))           {               TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "HeapGetMinNode failed.\n");               AMPS_InternalFree(poAMPSCacheEntryToAdd);               return AMPS_ERROR_FAILURE;           }              /*從堆中釋放*/           if(AMPS_SUCCESS != Cache_RemoveEntry(r_pvAMPSContext, r_pvAMPSCache, poHeapNodeLeastRefered->pvDataValue))           {               TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "Cache_RemoveEntry failed.\n");               AMPS_InternalFree(poAMPSCacheEntryToAdd);               return AMPS_ERROR_FAILURE;           }       }          /*將hash結點寫入*/       poAMPSCacheEntryToAdd->nHandle = HashTable_Insert(r_pvAMPSContext, poAMPSCache->pvHASHTable, poAMPSCacheEntryToAdd, &poAMPSCacheEntryToAdd->oHASHTableKey);       if(AMPS_INVALID_HANDLE == poAMPSCacheEntryToAdd->nHandle)       {           TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "HashTable_Insert failed.\n");                    AMPS_InternalFree(poAMPSCacheEntryToAdd);           return AMPS_ERROR_FAILURE;       }          /*將新結點寫入堆*/       TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Inserting new Entry into Heap.\n");       if(AMPS_ERROR_FAILURE == Heap_InsertMin(r_pvAMPSContext, poHeap, poAMPSCacheEntryToAdd, poAMPSCacheEntryToAdd->lEntryCreationTime))       {           TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "HeapInsertMin failed.\n");           AMPS_InternalFree(poAMPSCacheEntryToAdd);           return AMPS_ERROR_FAILURE;       }          TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n");       return AMPS_SUCCESS;   }      /*****************************************************************  函數名稱: Cache_RemoveEntry  功能描述: 從Cache中刪除數據  入參::        void* r_pvAMPSContext APMS應用上下文        void* r_pvAMPSCache Cache句柄        t_AMPSHashTableKey* r_poHASHTableKey Cache結點  出參:          返回值:        int  *****************************************************************/   int Cache_RemoveEntry(void* r_pvAMPSContext, void* r_pvAMPSCache, t_AMPSHashTableKey* r_poHASHTableKey)   {       t_AMPSCache* poAMPSCache = r_pvAMPSCache;       t_Heap* poHeap = poAMPSCache->pvHeap;       t_AMPSCacheEntry* poAMPSCacheEntryToRemove =  NULL;          TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");          /*查找cache結點*/       poAMPSCacheEntryToRemove = HashTable_Search(r_pvAMPSContext, poAMPSCache->pvHASHTable, r_poHASHTableKey);       if(NULL != poAMPSCacheEntryToRemove)       {           /*從hash表中刪除*/           TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Removing Entry from HashTable.\n");           if(AMPS_SUCCESS != HashTable_RemoveByHandle(r_pvAMPSContext, poAMPSCache->pvHASHTable, poAMPSCacheEntryToRemove->nHandle))           {               TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "HashTable_RemoveByHandle failed.\n");               return AMPS_ERROR_FAILURE;           }                 /*從堆中刪除*/           TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Removing Entry from Heap.\n");           if (AMPS_SUCCESS != Heap_RemoveNodeMin(r_pvAMPSContext, poHeap, poAMPSCacheEntryToRemove->lEntryCreationTime))           {               TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "Heap_RemoveNodeMin failed.\n");               return AMPS_ERROR_FAILURE;           }       }          TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n");       return AMPS_SUCCESS;   }      /*****************************************************************  函數名稱: Cache_UpdateEntry  功能描述: 更新Cache中的數據  入參::        void* r_pvAMPSContext APMS應用上下文        void* r_pvAMPSCache Cache句柄        t_AMPSHashTableKey* r_poHASHTableKey Cache結點  出參:          返回值:        int  *****************************************************************/   int Cache_UpdateEntry(void* r_pvAMPSContext, void* r_pvAMPSCache, t_AMPSHashTableKey* r_poHASHTableKey)   {       t_AMPSCache* poAMPSCache = r_pvAMPSCache;       t_Heap* poHeap = poAMPSCache->pvHeap;       t_AMPSCacheEntry* poAMPSCacheEntryToUpdate =  NULL;       long lOldEntryCreationTime = 0;          TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");          /*查找*/       poAMPSCacheEntryToUpdate = HashTable_Search(r_pvAMPSContext, poAMPSCache->pvHASHTable, r_poHASHTableKey);       if(NULL != poAMPSCacheEntryToUpdate)       {           /*以時間為key值*/           lOldEntryCreationTime = poAMPSCacheEntryToUpdate->lEntryCreationTime;           poAMPSCacheEntryToUpdate->lEntryCreationTime = SAPI_GetCurrentTimeInMilliSec(r_pvAMPSContext)/1000;              /*更新*/           if(AMPS_SUCCESS != Heap_UpdateNodeMinKey(r_pvAMPSContext, poHeap, lOldEntryCreationTime, poAMPSCacheEntryToUpdate->lEntryCreationTime))           {               TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "Heap_UpdateNodeMinKey failed.\n");               return AMPS_ERROR_FAILURE;           }       }       else       {           TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "HashTable_Search failed.\n");           return AMPS_ERROR_FAILURE;       }          TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n");       return AMPS_SUCCESS;   }      /*****************************************************************  函數名稱: Cache_LookupEntry  功能描述: 查找Cache中的數據  入參::        void* r_pvAMPSContext APMS應用上下文        void* r_pvAMPSCache Cache句柄        t_AMPSHashTableKey* r_poHASHTableKey Cache結點  出參:          返回值:        void* 查找到的結點  *****************************************************************/   void* Cache_LookupEntry(void* r_pvAMPSContext, void* r_pvAMPSCache, t_AMPSHashTableKey* r_poHASHTableKey)   {       t_AMPSCache* poAMPSCache = r_pvAMPSCache;       t_Heap* poHeap = poAMPSCache->pvHeap;       t_AMPSCacheEntry* poAMPSCacheEntryToFind =  NULL;       long lOldEntryCreationTime = 0;          TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");          poAMPSCacheEntryToFind = HashTable_Search(r_pvAMPSContext, poAMPSCache->pvHASHTable, r_poHASHTableKey);       if(NULL != poAMPSCacheEntryToFind)       {           lOldEntryCreationTime = poAMPSCacheEntryToFind->lEntryCreationTime;           poAMPSCacheEntryToFind->lEntryCreationTime = SAPI_GetCurrentTimeInMilliSec(r_pvAMPSContext)/1000;              /*找到後,更新一下時間*/           if(AMPS_SUCCESS != Heap_UpdateNodeMinKey(r_pvAMPSContext, poHeap, lOldEntryCreationTime, poAMPSCacheEntryToFind->lEntryCreationTime))           {               TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "Heap_UpdateNodeMinKey failed.\n");               return NULL;           }       }       else       {           TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "HashTable_Search failed.\n");           return NULL;       }          TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n");       return(poAMPSCacheEntryToFind->pvUserData);   }      /*****************************************************************  函數名稱: Cache_FreeEntry  功能描述: 釋放Cache結點  入參::        void* r_pvAMPSContext APMS應用上下文        void* r_pvAMPSCacheEntryToFree Cache結點  出參:          返回值:        int  *****************************************************************/   int Cache_FreeEntry(void* r_pvAMPSContext, void* r_pvAMPSCacheEntryToFree)   {       t_AMPSCacheEntry* poAMPSCacheEntryToFree =  r_pvAMPSCacheEntryToFree;       t_AMPSCache* poAMPSCache = poAMPSCacheEntryToFree->pvAMPSCache;          TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");          /*釋放前可能過注冊的回調函數做處理*/       if(NULL != poAMPSCache->pfAMPS_CacheProcessUserDataCallback)       {           TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "poAMPSCache->pfAMPS_CacheProcessUserDataCallback called.\n");           poAMPSCache->pfAMPS_CacheProcessUserDataCallback(r_pvAMPSContext, poAMPSCacheEntryToFree->pvUserData);       }          TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "AMPS_InternalFree called for poAMPSCacheEntryToRemove.\n");       AMPS_InternalFree(poAMPSCacheEntryToFree);          TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n");       return AMPS_SUCCESS;   }     #include "AMPS_Core.h" #include "AMPS_Defines.h" #include "AMPS_MemMgt.h" #include "AMPS_LinkList.h" #include "AMPS_Cache.h"   /***************************************************************** 函數名稱: Cache_Init 功能描述: Cache模塊初始化 入參::       void* r_pvAMPSContext APMS應用上下文       int r_nCacheSize  Cache大小       AMPS_CacheProcessUserDataCallback r_pfAMPS_CacheProcessUserDataCallback               出參:        返回值:       void* Cache句柄   *****************************************************************/ void* Cache_Init(void* r_pvAMPSContext, int r_nCacheSize, AMPS_CacheProcessUserDataCallback r_pfAMPS_CacheProcessUserDataCallback) { t_AMPSCache* poAMPSCache = NULL;       TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");       /*分配一個Cache對象*/     poAMPSCache = AMPS_InternalMalloc(sizeof(t_AMPSCache));     if (NULL == poAMPSCache)     {         TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "AMPS_InternalMalloc failed for poAMPSCache.\n");         return NULL;     }       /*指定Cache大小*/ poAMPSCache->nCacheSize = r_nCacheSize;       /*Cache中結點數據處理回調函數*/ TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Set poAMPSCache->pfAMPS_CacheProcessUserDataCallback to user given function.\n"); poAMPSCache->pfAMPS_CacheProcessUserDataCallback = r_pfAMPS_CacheProcessUserDataCallback;       /*創建Cache所使用的Hash表,用於查找*/     poAMPSCache->pvHASHTable = HashTable_Create(r_pvAMPSContext, poAMPSCache->nCacheSize, NULL);     if (NULL == poAMPSCache->pvHASHTable)     {         TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "HashTable_Create failed.\n");         return NULL;     }       /*創建Cache所使用堆,用於增、刪、改*/     poAMPSCache->pvHeap = Heap_Init(r_pvAMPSContext, poAMPSCache->nCacheSize, Cache_FreeEntry);     if(NULL == poAMPSCache->pvHeap)     {         TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "HeapInit failed.\n");         return NULL;     }   TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n"); return poAMPSCache; }   /***************************************************************** 函數名稱: Cache_Cleanup 功能描述: Cache模塊銷毀 入參::       void* r_pvAMPSContext APMS應用上下文       void* r_pvAMPSCache Cache句柄        出參:        返回值:       void *****************************************************************/ void Cache_Cleanup(void* r_pvAMPSContext, void* r_pvAMPSCache) { t_AMPSCache* poAMPSCache = r_pvAMPSCache;       TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");       /*釋放Hash表*/     TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "HashTable_Delete called.\n");     HashTable_Delete(r_pvAMPSContext, poAMPSCache->pvHASHTable);       /*釋放堆*/     TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Heap_Cleanup called.\n");     Heap_Cleanup(r_pvAMPSContext, poAMPSCache->pvHeap);       /*釋放Cache句柄*/     TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "AMPS_InternalFree called for poAMPSCache.\n");     AMPS_InternalFree(poAMPSCache);       TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n"); }   /***************************************************************** 函數名稱: Cache_AddEntry 功能描述: 向Cache內寫數 入參::       void* r_pvAMPSContext APMS應用上下文       void* r_pvAMPSCache Cache句柄       t_AMPSHashTableKey* r_poHASHTableKey Cache結點       void* r_pvData 結點數 出參:        返回值:       void *****************************************************************/ int Cache_AddEntry(void* r_pvAMPSContext, void* r_pvAMPSCache, t_AMPSHashTableKey* r_poHASHTableKey, void* r_pvData) { t_AMPSCache* poAMPSCache = r_pvAMPSCache;     t_Heap* poHeap = poAMPSCache->pvHeap; t_AMPSCacheEntry* poAMPSCacheEntryToAdd =  NULL;   TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");       /*分配一個Cache結點*/ poAMPSCacheEntryToAdd = AMPS_InternalMalloc(sizeof(t_AMPSCacheEntry));     if (NULL == poAMPSCacheEntryToAdd)     {         TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "AMPS_InternalMalloc failed for poAMPSCacheEntryToAdd.\n");         return AMPS_ERROR_FAILURE;     }          /*結點賦值*/ poAMPSCacheEntryToAdd->oHASHTableKey = *r_poHASHTableKey; /*存放hash結點*/ poAMPSCacheEntryToAdd->pvAMPSCache = poAMPSCache; /*存放Cache句柄*/ poAMPSCacheEntryToAdd->pvUserData = r_pvData; /*存放數據*/     /*結點創建時間,這個時間是cache中hash的key值*/     poAMPSCacheEntryToAdd->lEntryCreationTime = SAPI_GetCurrentTimeInMilliSec(r_pvAMPSContext)/1000; //Set creation time       /*Cache已滿*/ if(poHeap->nIndex >= poAMPSCache->nCacheSize) //Cache is full, remove Entry from Cache     {         t_HeapNode* poHeapNodeLeastRefered = NULL;           TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Cache is full, Remove Entry from Cache.\n");           /*獲取最小堆根結點,即進入Cache最早的結點*/         if(AMPS_SUCCESS != Heap_GetMinNode(r_pvAMPSContext, poHeap, (void**)&poHeapNodeLeastRefered))         {             TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "HeapGetMinNode failed.\n"); AMPS_InternalFree(poAMPSCacheEntryToAdd);             return AMPS_ERROR_FAILURE;         }           /*從堆中釋放*/ if(AMPS_SUCCESS != Cache_RemoveEntry(r_pvAMPSContext, r_pvAMPSCache, poHeapNodeLeastRefered->pvDataValue)) {             TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "Cache_RemoveEntry failed.\n"); AMPS_InternalFree(poAMPSCacheEntryToAdd);             return AMPS_ERROR_FAILURE; }     }       /*將hash結點寫入*/ poAMPSCacheEntryToAdd->nHandle = HashTable_Insert(r_pvAMPSContext, poAMPSCache->pvHASHTable, poAMPSCacheEntryToAdd, &poAMPSCacheEntryToAdd->oHASHTableKey);     if(AMPS_INVALID_HANDLE == poAMPSCacheEntryToAdd->nHandle)     {         TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "HashTable_Insert failed.\n");          AMPS_InternalFree(poAMPSCacheEntryToAdd);         return AMPS_ERROR_FAILURE;     }       /*將新結點寫入堆*/     TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Inserting new Entry into Heap.\n"); if(AMPS_ERROR_FAILURE == Heap_InsertMin(r_pvAMPSContext, poHeap, poAMPSCacheEntryToAdd, poAMPSCacheEntryToAdd->lEntryCreationTime))     {         TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "HeapInsertMin failed.\n"); AMPS_InternalFree(poAMPSCacheEntryToAdd);         return AMPS_ERROR_FAILURE;     }       TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n"); return AMPS_SUCCESS; }   /***************************************************************** 函數名稱: Cache_RemoveEntry 功能描述: 從Cache中刪除數據 入參::       void* r_pvAMPSContext APMS應用上下文       void* r_pvAMPSCache Cache句柄       t_AMPSHashTableKey* r_poHASHTableKey Cache結點 出參:        返回值:       int *****************************************************************/ int Cache_RemoveEntry(void* r_pvAMPSContext, void* r_pvAMPSCache, t_AMPSHashTableKey* r_poHASHTableKey) { t_AMPSCache* poAMPSCache = r_pvAMPSCache;     t_Heap* poHeap = poAMPSCache->pvHeap; t_AMPSCacheEntry* poAMPSCacheEntryToRemove =  NULL;   TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");       /*查找cache結點*/ poAMPSCacheEntryToRemove = HashTable_Search(r_pvAMPSContext, poAMPSCache->pvHASHTable, r_poHASHTableKey); if(NULL != poAMPSCacheEntryToRemove) {         /*從hash表中刪除*/ TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Removing Entry from HashTable.\n"); if(AMPS_SUCCESS != HashTable_RemoveByHandle(r_pvAMPSContext, poAMPSCache->pvHASHTable, poAMPSCacheEntryToRemove->nHandle)) { TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "HashTable_RemoveByHandle failed.\n"); return AMPS_ERROR_FAILURE; }             /*從堆中刪除*/ TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Removing Entry from Heap.\n"); if (AMPS_SUCCESS != Heap_RemoveNodeMin(r_pvAMPSContext, poHeap, poAMPSCacheEntryToRemove->lEntryCreationTime)) { TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "Heap_RemoveNodeMin failed.\n"); return AMPS_ERROR_FAILURE; } }   TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n"); return AMPS_SUCCESS; }   /***************************************************************** 函數名稱: Cache_UpdateEntry 功能描述: 更新Cache中的數據 入參::       void* r_pvAMPSContext APMS應用上下文       void* r_pvAMPSCache Cache句柄       t_AMPSHashTableKey* r_poHASHTableKey Cache結點 出參:        返回值:       int *****************************************************************/ int Cache_UpdateEntry(void* r_pvAMPSContext, void* r_pvAMPSCache, t_AMPSHashTableKey* r_poHASHTableKey) { t_AMPSCache* poAMPSCache = r_pvAMPSCache;     t_Heap* poHeap = poAMPSCache->pvHeap; t_AMPSCacheEntry* poAMPSCacheEntryToUpdate =  NULL;     long lOldEntryCreationTime = 0;   TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");       /*查找*/ poAMPSCacheEntryToUpdate = HashTable_Search(r_pvAMPSContext, poAMPSCache->pvHASHTable, r_poHASHTableKey); if(NULL != poAMPSCacheEntryToUpdate) {         /*以時間為key值*/ lOldEntryCreationTime = poAMPSCacheEntryToUpdate->lEntryCreationTime; poAMPSCacheEntryToUpdate->lEntryCreationTime = SAPI_GetCurrentTimeInMilliSec(r_pvAMPSContext)/1000;           /*更新*/ if(AMPS_SUCCESS != Heap_UpdateNodeMinKey(r_pvAMPSContext, poHeap, lOldEntryCreationTime, poAMPSCacheEntryToUpdate->lEntryCreationTime)) { TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "Heap_UpdateNodeMinKey failed.\n"); return AMPS_ERROR_FAILURE; } } else { TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "HashTable_Search failed.\n"); return AMPS_ERROR_FAILURE; }   TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n"); return AMPS_SUCCESS; }   /***************************************************************** 函數名稱: Cache_LookupEntry 功能描述: 查找Cache中的數據 入參::       void* r_pvAMPSContext APMS應用上下文       void* r_pvAMPSCache Cache句柄       t_AMPSHashTableKey* r_poHASHTableKey Cache結點 出參:        返回值:       void* 查找到的結點 *****************************************************************/ void* Cache_LookupEntry(void* r_pvAMPSContext, void* r_pvAMPSCache, t_AMPSHashTableKey* r_poHASHTableKey) { t_AMPSCache* poAMPSCache = r_pvAMPSCache;     t_Heap* poHeap = poAMPSCache->pvHeap; t_AMPSCacheEntry* poAMPSCacheEntryToFind =  NULL;     long lOldEntryCreationTime = 0;       TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");   poAMPSCacheEntryToFind = HashTable_Search(r_pvAMPSContext, poAMPSCache->pvHASHTable, r_poHASHTableKey); if(NULL != poAMPSCacheEntryToFind) { lOldEntryCreationTime = poAMPSCacheEntryToFind->lEntryCreationTime; poAMPSCacheEntryToFind->lEntryCreationTime = SAPI_GetCurrentTimeInMilliSec(r_pvAMPSContext)/1000;           /*找到後,更新一下時間*/ if(AMPS_SUCCESS != Heap_UpdateNodeMinKey(r_pvAMPSContext, poHeap, lOldEntryCreationTime, poAMPSCacheEntryToFind->lEntryCreationTime)) { TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "Heap_UpdateNodeMinKey failed.\n"); return NULL; } } else { TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "HashTable_Search failed.\n"); return NULL; }   TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n"); return(poAMPSCacheEntryToFind->pvUserData); }   /***************************************************************** 函數名稱: Cache_FreeEntry 功能描述: 釋放Cache結點 入參::       void* r_pvAMPSContext APMS應用上下文       void* r_pvAMPSCacheEntryToFree Cache結點 出參:        返回值:       int *****************************************************************/ int Cache_FreeEntry(void* r_pvAMPSContext, void* r_pvAMPSCacheEntryToFree) { t_AMPSCacheEntry* poAMPSCacheEntryToFree =  r_pvAMPSCacheEntryToFree; t_AMPSCache* poAMPSCache = poAMPSCacheEntryToFree->pvAMPSCache;   TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");       /*釋放前可能過注冊的回調函數做處理*/ if(NULL != poAMPSCache->pfAMPS_CacheProcessUserDataCallback) { TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "poAMPSCache->pfAMPS_CacheProcessUserDataCallback called.\n"); poAMPSCache->pfAMPS_CacheProcessUserDataCallback(r_pvAMPSContext, poAMPSCacheEntryToFree->pvUserData); }   TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "AMPS_InternalFree called for poAMPSCacheEntryToRemove.\n"); AMPS_InternalFree(poAMPSCacheEntryToFree);   TRACE( CACHE_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n"); return AMPS_SUCCESS; }      

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved