summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_cache.h
blob: 567b5581214a446ee4c6860b6bef243e35644db0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122

/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) NGINX, Inc.
 */

#ifndef _NXT_CACHE_INCLUDED_
#define _NXT_CACHE_INCLUDED_


typedef struct nxt_cache_query_s       nxt_cache_query_t;
typedef struct nxt_cache_query_wait_s  nxt_cache_query_wait_t;


typedef struct {
    uint32_t                  shared;        /* 1 bit */
    nxt_thread_spinlock_t     lock;

    nxt_lvlhsh_t              lvlhsh;
    const nxt_lvlhsh_proto_t  *proto;
    void                      *pool;

    nxt_queue_t               expiry_queue;

    nxt_queue_t               free_nodes;
    uint32_t                  nfree_nodes;

    uint32_t                  nfree_query_wait;
    nxt_cache_query_wait_t    *free_query_wait;

    uint64_t                  start_time;

    /* STUB: use nxt_lvlhsh_proto_t */
    void                      *(*alloc)(void *data, size_t size);
    void                      (*free)(void *data, void *p);
    void                      *data;

    nxt_work_handler_t        delete_handler;
} nxt_cache_t;


typedef struct {
    u_char                    *key_data;

    uint16_t                  key_len;       /* 16 bits */
    uint8_t                   uses;          /* 8 bits */
    uint8_t                   updating:1;
    uint8_t                   deleted:1;

    uint32_t                  count;

    /* Times relative to the cache->start_time. */
    uint32_t                  expiry;
    uint32_t                  accessed;

    nxt_off_t                 size;

    nxt_queue_link_t          link;

    nxt_cache_query_wait_t    *waiting;
} nxt_cache_node_t;


struct nxt_cache_query_wait_s {
    nxt_cache_query_t         *query;
    nxt_cache_query_wait_t    *next;

    uint8_t                   busy;          /* 1 bit */
    uint8_t                   deleted;       /* 1 bit */

    nxt_pid_t                 pid;
    nxt_event_engine_t        *engine;
    nxt_work_handler_t        handler;
    nxt_cache_t               *cache;
};


typedef struct {
    nxt_work_handler_t        nocache_handler;
    nxt_work_handler_t        ready_handler;
    nxt_work_handler_t        stale_handler;
    nxt_work_handler_t        update_stale_handler;
    nxt_work_handler_t        update_handler;
    nxt_work_handler_t        timeout_handler;
    nxt_work_handler_t        error_handler;
} nxt_cache_query_state_t;


struct nxt_cache_query_s {
    u_char                    *key_data;

    uint16_t                  key_len;       /* 16 bits */
#if (NXT_64_BIT)
    uint8_t                   hold;          /* 1 bit */
    uint8_t                   use_stale;     /* 1 bit */
    uint8_t                   update_stale;  /* 1 bit */
    uint8_t                   stale;         /* 1 bit */
#else
    uint8_t                   hold:1;
    uint8_t                   use_stale:1;
    uint8_t                   update_stale:1;
    uint8_t                   stale:1;
#endif

    nxt_cache_node_t          *node;
    nxt_cache_query_t         *next;
    nxt_cache_query_state_t   *state;

    nxt_time_t                now;

    nxt_msec_t                timeout;
    nxt_timer_t               timer;
};


NXT_EXPORT void nxt_cache_init(nxt_cache_t *cache);
NXT_EXPORT void nxt_cache_query(nxt_cache_t *cache, nxt_cache_query_t *q);
NXT_EXPORT void nxt_cache_release(nxt_cache_t *cache, nxt_cache_query_t *q);
NXT_EXPORT nxt_int_t nxt_cache_update(nxt_cache_t *cache, nxt_cache_query_t *q);


#endif /* _NXT_CACHE_INCLUDED_ */