1 2 /* 3 * Copyright (C) Igor Sysoev 4 * Copyright (C) NGINX, Inc. 5 */ 6 7 #ifndef _NXT_CACHE_INCLUDED_ 8 #define _NXT_CACHE_INCLUDED_ 9 10 11 typedef struct nxt_cache_query_s nxt_cache_query_t; 12 typedef struct nxt_cache_query_wait_s nxt_cache_query_wait_t; 13 14 15 typedef struct { 16 uint32_t shared; /* 1 bit */ 17 nxt_thread_spinlock_t lock; 18 19 nxt_lvlhsh_t lvlhsh; 20 const nxt_lvlhsh_proto_t *proto; 21 void *pool; 22 23 nxt_queue_t expiry_queue; 24 25 nxt_queue_t free_nodes; 26 uint32_t nfree_nodes; 27 28 uint32_t nfree_query_wait; 29 nxt_cache_query_wait_t *free_query_wait; 30 31 uint64_t start_time; 32 33 /* STUB: use nxt_lvlhsh_proto_t */ 34 void *(*alloc)(void *data, size_t size); 35 void (*free)(void *data, void *p); 36 void *data; 37 38 nxt_work_handler_t delete_handler; 39 } nxt_cache_t; 40 41 42 typedef struct { 43 u_char *key_data; 44 45 uint16_t key_len; /* 16 bits */ 46 uint8_t uses; /* 8 bits */ 47 uint8_t updating:1; 48 uint8_t deleted:1; 49 50 uint32_t count; 51 52 /* Times relative to the cache->start_time. */ 53 uint32_t expiry; 54 uint32_t accessed; 55 56 nxt_off_t size; 57 58 nxt_queue_link_t link; 59 60 nxt_cache_query_wait_t *waiting; 61 } nxt_cache_node_t; 62 63 64 struct nxt_cache_query_wait_s { 65 nxt_cache_query_t *query; 66 nxt_cache_query_wait_t *next; 67 68 uint8_t busy; /* 1 bit */ 69 uint8_t deleted; /* 1 bit */ 70 71 nxt_pid_t pid; 72 nxt_event_engine_t *engine; 73 nxt_work_handler_t handler; 74 nxt_cache_t *cache; 75 }; 76 77 78 typedef struct { 79 nxt_work_handler_t nocache_handler; 80 nxt_work_handler_t ready_handler; 81 nxt_work_handler_t stale_handler; 82 nxt_work_handler_t update_stale_handler; 83 nxt_work_handler_t update_handler; 84 nxt_work_handler_t timeout_handler; 85 nxt_work_handler_t error_handler; 86 } nxt_cache_query_state_t; 87 88 89 struct nxt_cache_query_s { 90 u_char *key_data; 91 92 uint16_t key_len; /* 16 bits */ 93 #if (NXT_64_BIT) 94 uint8_t hold; /* 1 bit */ 95 uint8_t use_stale; /* 1 bit */ 96 uint8_t update_stale; /* 1 bit */ 97 uint8_t stale; /* 1 bit */ 98 #else 99 uint8_t hold:1; 100 uint8_t use_stale:1; 101 uint8_t update_stale:1; 102 uint8_t stale:1; 103 #endif 104 105 nxt_cache_node_t *node; 106 nxt_cache_query_t *next; 107 nxt_cache_query_state_t *state; 108 109 nxt_time_t now; 110 111 nxt_msec_t timeout; 112 nxt_timer_t timer; 113 }; 114 115 116 NXT_EXPORT void nxt_cache_init(nxt_cache_t *cache); 117 NXT_EXPORT void nxt_cache_query(nxt_cache_t *cache, nxt_cache_query_t *q); 118 NXT_EXPORT void nxt_cache_release(nxt_cache_t *cache, nxt_cache_query_t *q); 119 NXT_EXPORT nxt_int_t nxt_cache_update(nxt_cache_t *cache, nxt_cache_query_t *q); 120 121 122 #endif /* _NXT_CACHE_INCLUDED_ */ 123