libcortex
Event loop library following a "batteries included" approach
Loading...
Searching...
No Matches
cache.h
1#ifndef _CRTX_CACHE_H
2#define _CRTX_CACHE_H
3
4/*
5 * Mario Kicherer (dev@kicherer.org) 2016
6 *
7 */
8
9#include "dict.h"
10
11struct crtx_cache;
12struct crtx_cache_entry;
13struct crtx_cache_task;
14
15typedef int (*create_key_cb_t)(struct crtx_cache *cache, struct crtx_dict_item *key, struct crtx_event *event);
16typedef struct crtx_dict_item * (*match_cb_t)(struct crtx_cache *cache, struct crtx_dict_item *key, struct crtx_event *event);
17typedef int (*hit_cb_t)(struct crtx_cache *cache, struct crtx_dict_item *key, struct crtx_event *event, struct crtx_dict_item *c_entry);
18typedef int (*miss_cb_t)(struct crtx_cache *cache, struct crtx_dict_item *key, struct crtx_event *event, struct crtx_dict_item *c_entry);
19typedef int (*add_cb_t)(struct crtx_cache *cache, struct crtx_dict_item *key, struct crtx_event *event);
20typedef void (*get_time_cb_t)(struct crtx_dict_item *item);
21
22
23// An entry in the simple layout cache contains only the value.
24// In a non-simple layout the entry contains a dictionary which may contain
25// additional fields like number of hits or time since last hist
26#define CRTX_CACHE_SIMPLE_LAYOUT 1<<0
27#define CRTX_CACHE_NO_EXT_FIELDS 1<<1
28// #define CRTX_CACHE_REGEXP 1<<2
29
30struct crtx_cache {
31 char flags;
32
33 struct crtx_dict *dict;
34
35 // pointers into $dict
36 struct crtx_dict *config;
37 struct crtx_dict *entries;
38
39 get_time_cb_t get_time;
40
41 pthread_mutex_t mutex;
42
43 create_key_cb_t create_key;
44 match_cb_t match_event;
45 hit_cb_t on_hit;
46 miss_cb_t on_miss;
47 add_cb_t on_add; // can prevent adding entries to the cache
48
49 void *userdata;
50
51 struct crtx_task *task;
52
53 #ifndef CRTX_REDUCED_SIZE
54 void *reserved1;
55 #endif
56};
57
58struct crtx_dict_item * crtx_cache_match_cb_t_regex(struct crtx_cache *cache, struct crtx_dict_item *key, struct crtx_event *event);
59struct crtx_dict_item * crtx_cache_match_cb_t_strcmp(struct crtx_cache *cache, struct crtx_dict_item *key, struct crtx_event *event);
60
61int crtx_cache_no_add(struct crtx_cache *cache, struct crtx_dict_item *key, struct crtx_event *event);
62int crtx_cache_on_hit(struct crtx_cache *cache, struct crtx_dict_item *key, struct crtx_event *event, struct crtx_dict_item *c_entry);
63int crtx_cache_add_on_miss(struct crtx_cache *cache, struct crtx_dict_item *key, struct crtx_event *event, struct crtx_dict_item *c_entry);
64int crtx_cache_update_on_hit(struct crtx_cache *cache, struct crtx_dict_item *key, struct crtx_event *event, struct crtx_dict_item *c_entry);
65
66int crtx_create_cache_task(struct crtx_task **task, char *id, create_key_cb_t create_key);
67void crtx_free_cache_task(struct crtx_task *task);
68
69struct crtx_dict_item * crtx_cache_add_entry(struct crtx_cache *cache, struct crtx_dict_item *key, struct crtx_event *event, struct crtx_dict_item *entry_data);
70int crtx_cache_task_handler(struct crtx_event *event, void *userdata, void **sessiondata);
71char crtx_load_cache(struct crtx_cache *cache, char *path);
72void crtx_cache_remove_entry(struct crtx_cache *cache, struct crtx_dict_item *key);
73
74#endif
Definition cache.h:30
Definition dict.h:35
Definition dict.h:61
an event that is emitted by a listener crtx_listener_base
Definition core.h:59
a task basically represents a function that will be executed for an crtx_event
Definition core.h:91