Atrinik Server  4.0
Functions | Variables
cache.c File Reference
#include <global.h>
#include <plugin.h>

Go to the source code of this file.

Functions

static int cache_compare (const void *one, const void *two)
 
cache_structcache_find (shstr *key)
 
int cache_add (const char *key, void *ptr, uint32_t flags)
 
int cache_remove (shstr *key)
 
void cache_remove_all (void)
 
void cache_remove_by_flags (uint32_t flags)
 

Variables

static cache_structcache = NULL
 
static size_t num_cache = 0
 

Detailed Description

Implements generic caching of any pointer identified by a unique key.

API usage:

char *str = estrdup("hello world");
// CACHE_FLAG_AUTOFREE will automatically free the pointer.
cache_add("cache_test", str, CACHE_FLAG_AUTOFREE);
// The cache code stores the keys as shared strings, so you must attempt
// to find the string first.
res = cache_find(find_string("cache_test"));
// Since it was added before, it should not be NULL here.
if (res)
{
// The cache uses void pointers, so you need to cast the returned pointer
// back to what it was.
printf("Found cache entry:\n%s\n", (char *) res->ptr);
}
// Remove the cache entry: after this call, 'str' is pointing to freed
memory,
// since CACHE_FLAG_AUTOFREE was set.
cache_remove(find_string("cache_test"));

Definition in file cache.c.

Function Documentation

int cache_add ( const char *  key,
void *  ptr,
uint32_t  flags 
)

Add an entry to the cache.

Parameters
keyUnique identified for the cache entry.
ptrPointer to store; must not be freed.
flagsA combination of Cache flags.
Returns
1 on success, 0 on failure (NULL ptr, or cache entry with name 'key' already exists).

Definition at line 121 of file cache.c.

static int cache_compare ( const void *  one,
const void *  two 
)
static

Comparison function for binary search in cache_find().

Definition at line 68 of file cache.c.

cache_struct* cache_find ( shstr *  key)

Attempt to find a cache entry, identified by 'key'.

Parameters
keyCache entry to find.
Returns
Pointer to the cache entry, NULL if there is no such entry.

Definition at line 95 of file cache.c.

int cache_remove ( shstr *  key)

Remove a cache entry identified by 'key'.

Parameters
keyWhat cache entry to remove.
Returns
1 on success, 0 on failure (cache entry not found).

Definition at line 167 of file cache.c.

void cache_remove_all ( void  )

Remove all cache entries.

Definition at line 205 of file cache.c.

void cache_remove_by_flags ( uint32_t  flags)

Remove all cache entries identified by (a combination of) 'flags'.

Parameters
flagsOne or a combination of Cache flags.

Definition at line 222 of file cache.c.

Variable Documentation

cache_struct* cache = NULL
static

Array of the cached entries.

Definition at line 61 of file cache.c.

size_t num_cache = 0
static

Number of entries in cache.

Definition at line 63 of file cache.c.