这里讲的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" &nbs
补充:软件开发 , C++ ,