1 /* 2 cache.h -- Generic cache routines headers and definitions file. 3 Copyright (C) 2003 Roy Keene 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 2 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program; if not, write to the Free Software 17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 19 email: tcpcgi@rkeene.org 20 21 */ 22 23 #ifndef CACHE_H_RSK 24 #define CACHE_H_RSK 25 /* 26 * Generic cache routines -- designed for the LAN Block Device, but 27 * should be usable for almost anything (hopefully). 28 */ 29 30 31 #include <sys/types.h> 32 #include <inttypes.h> 33 #include <unistd.h> 34 #include <stdio.h> 35 #include <time.h> 36 37 struct cache_item; 38 39 struct cache_item { 40 void *data; /* Pointer to the actual data. [REQUIRED] */ 41 void *key; /* (Start) Key to index this record with. [REQUIRED] */ 42 uint8_t keylen; /* Length of key. [REQUIRED] */ 43 uint32_t datalen; /* datalen is optional, and only there for convience. */ 44 time_t expires; /* Absolute time when this record expires. */ 45 int (*cleanup)(); /* Clean-up routine, executed when this record expires, called as: cleanup(data,key,keylen); */ 46 struct cache_item *_next; /* Pointer to the next item, so we can be in a list. [REQUIRED] */ 47 struct cache_item *_prev; /* Pointer to the previous item, so we can delete more easily. [REQUIRED] */ 48 }; 49 50 typedef struct cache_item cachei_t; 51 52 typedef struct { 53 cachei_t **hashtable; 54 uint32_t hashsize; 55 } cache_t; 56 57 cache_t *cache_create(int num); 58 int cache_destroy(cache_t *c); 59 void *cache_add(cache_t *c, void *key, uint8_t keylen, void *data, uint32_t datalen, time_t ttl, void *cleanup); 60 void *cache_delete(cache_t *c, void *key, uint8_t keylen, uint32_t *datalen, int cleanup); 61 void *cache_find(cache_t *c, void *key, uint8_t keylen, uint32_t *datalen); 62 int cache_save(cache_t *c, FILE *fp, int fd); 63 cache_t *cache_load(FILE *fp, int fd); 64 #if defined(DEBUG) || defined(CACHE_DEBUG) 65 void cache_print(cache_t *c); 66 #endif 67 68 #endif |