Atrinik Server 2.5
Functions | Variables
server/cache.c File Reference
#include <global.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 flags)
int cache_remove (shstr *key)
void cache_remove_all ()
void cache_remove_by_flags (uint32 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 = strdup_local("hello world");
cache_struct *res;

// 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  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 119 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 65 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 97 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 164 of file cache.c.

void cache_remove_all ( )

Remove all cache entries.

Definition at line 203 of file cache.c.

void cache_remove_by_flags ( uint32  flags)

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

Parameters:
flagsOne or a combination of Cache flags.

Definition at line 215 of file cache.c.


Variable Documentation

cache_struct* cache = NULL [static]

Array of the cached entries.

Definition at line 59 of file cache.c.

size_t num_cache = 0 [static]

Number of entries in cache.

Definition at line 61 of file cache.c.