@@ -19,44 +19,121 @@ local ngx_shared = ngx.shared
19
19
local getmetatable = getmetatable
20
20
local FFI_ERROR = base .FFI_ERROR
21
21
local FFI_DECLINED = base .FFI_DECLINED
22
+ local subsystem = ngx .config .subsystem
22
23
23
24
24
- ffi .cdef [[
25
- int ngx_http_lua_ffi_shdict_get (void * zone , const unsigned char * key ,
26
- size_t key_len , int * value_type , unsigned char ** str_value_buf ,
27
- size_t * str_value_len , double * num_value , int * user_flags ,
28
- int get_stale , int * is_stale , char ** errmsg );
25
+ local ngx_lua_ffi_shdict_get
26
+ local ngx_lua_ffi_shdict_incr
27
+ local ngx_lua_ffi_shdict_store
28
+ local ngx_lua_ffi_shdict_flush_all
29
+ local ngx_lua_ffi_shdict_get_ttl
30
+ local ngx_lua_ffi_shdict_set_expire
31
+ local ngx_lua_ffi_shdict_capacity
32
+ local ngx_lua_ffi_shdict_free_space
29
33
30
- int ngx_http_lua_ffi_shdict_incr (void * zone , const unsigned char * key ,
31
- size_t key_len , double * value , char ** err , int has_init , double init ,
32
- long init_ttl , int * forcible );
33
34
34
- int ngx_http_lua_ffi_shdict_store (void * zone , int op ,
35
- const unsigned char * key , size_t key_len , int value_type ,
36
- const unsigned char * str_value_buf , size_t str_value_len ,
37
- double num_value , long exptime , int user_flags , char ** errmsg ,
38
- int * forcible );
35
+ if subsystem == ' http' then
36
+ ffi .cdef [[
37
+ int ngx_http_lua_ffi_shdict_get (void * zone , const unsigned char * key ,
38
+ size_t key_len , int * value_type , unsigned char ** str_value_buf ,
39
+ size_t * str_value_len , double * num_value , int * user_flags ,
40
+ int get_stale , int * is_stale , char ** errmsg );
41
+
42
+ int ngx_http_lua_ffi_shdict_incr (void * zone , const unsigned char * key ,
43
+ size_t key_len , double * value , char ** err , int has_init ,
44
+ double init , long init_ttl , int * forcible );
45
+
46
+ int ngx_http_lua_ffi_shdict_store (void * zone , int op ,
47
+ const unsigned char * key , size_t key_len , int value_type ,
48
+ const unsigned char * str_value_buf , size_t str_value_len ,
49
+ double num_value , long exptime , int user_flags , char ** errmsg ,
50
+ int * forcible );
51
+
52
+ int ngx_http_lua_ffi_shdict_flush_all (void * zone );
53
+
54
+ long ngx_http_lua_ffi_shdict_get_ttl (void * zone ,
55
+ const unsigned char * key , size_t key_len );
39
56
40
- int ngx_http_lua_ffi_shdict_flush_all (void * zone );
57
+ int ngx_http_lua_ffi_shdict_set_expire (void * zone ,
58
+ const unsigned char * key , size_t key_len , long exptime );
41
59
42
- long ngx_http_lua_ffi_shdict_get_ttl (void * zone ,
43
- const unsigned char * key , size_t key_len );
60
+ size_t ngx_http_lua_ffi_shdict_capacity (void * zone );
61
+ ]]
62
+
63
+ ngx_lua_ffi_shdict_get = C .ngx_http_lua_ffi_shdict_get
64
+ ngx_lua_ffi_shdict_incr = C .ngx_http_lua_ffi_shdict_incr
65
+ ngx_lua_ffi_shdict_store = C .ngx_http_lua_ffi_shdict_store
66
+ ngx_lua_ffi_shdict_flush_all = C .ngx_http_lua_ffi_shdict_flush_all
67
+ ngx_lua_ffi_shdict_get_ttl = C .ngx_http_lua_ffi_shdict_get_ttl
68
+ ngx_lua_ffi_shdict_set_expire = C .ngx_http_lua_ffi_shdict_set_expire
69
+ ngx_lua_ffi_shdict_capacity = C .ngx_http_lua_ffi_shdict_capacity
44
70
45
- int ngx_http_lua_ffi_shdict_set_expire (void * zone ,
46
- const unsigned char * key , size_t key_len , long exptime );
71
+ if not pcall (function ()
72
+ return C .ngx_http_lua_ffi_shdict_free_space
73
+ end )
74
+ then
75
+ ffi .cdef [[
76
+ size_t ngx_http_lua_ffi_shdict_free_space (void * zone );
77
+ ]]
78
+ end
47
79
48
- size_t ngx_http_lua_ffi_shdict_capacity (void * zone );
49
- ]]
80
+ ngx_lua_ffi_shdict_free_space = C .ngx_http_lua_ffi_shdict_free_space
81
+
82
+ elseif subsystem == ' stream' then
50
83
51
- if not pcall (function () return C .ngx_http_lua_ffi_shdict_free_space end ) then
52
84
ffi .cdef [[
53
- size_t ngx_http_lua_ffi_shdict_free_space (void * zone );
85
+ int ngx_stream_lua_ffi_shdict_get (void * zone , const unsigned char * key ,
86
+ size_t key_len , int * value_type , unsigned char ** str_value_buf ,
87
+ size_t * str_value_len , double * num_value , int * user_flags ,
88
+ int get_stale , int * is_stale , char ** errmsg );
89
+
90
+ int ngx_stream_lua_ffi_shdict_incr (void * zone , const unsigned char * key ,
91
+ size_t key_len , double * value , char ** err , int has_init ,
92
+ double init , long init_ttl , int * forcible );
93
+
94
+ int ngx_stream_lua_ffi_shdict_store (void * zone , int op ,
95
+ const unsigned char * key , size_t key_len , int value_type ,
96
+ const unsigned char * str_value_buf , size_t str_value_len ,
97
+ double num_value , long exptime , int user_flags , char ** errmsg ,
98
+ int * forcible );
99
+
100
+ int ngx_stream_lua_ffi_shdict_flush_all (void * zone );
101
+
102
+ long ngx_stream_lua_ffi_shdict_get_ttl (void * zone ,
103
+ const unsigned char * key , size_t key_len );
104
+
105
+ int ngx_stream_lua_ffi_shdict_set_expire (void * zone ,
106
+ const unsigned char * key , size_t key_len , long exptime );
107
+
108
+ size_t ngx_stream_lua_ffi_shdict_capacity (void * zone );
54
109
]]
110
+
111
+ ngx_lua_ffi_shdict_get = C .ngx_stream_lua_ffi_shdict_get
112
+ ngx_lua_ffi_shdict_incr = C .ngx_stream_lua_ffi_shdict_incr
113
+ ngx_lua_ffi_shdict_store = C .ngx_stream_lua_ffi_shdict_store
114
+ ngx_lua_ffi_shdict_flush_all = C .ngx_stream_lua_ffi_shdict_flush_all
115
+ ngx_lua_ffi_shdict_get_ttl = C .ngx_stream_lua_ffi_shdict_get_ttl
116
+ ngx_lua_ffi_shdict_set_expire = C .ngx_stream_lua_ffi_shdict_set_expire
117
+ ngx_lua_ffi_shdict_capacity = C .ngx_stream_lua_ffi_shdict_capacity
118
+
119
+ if not pcall (function ()
120
+ return C .ngx_stream_lua_ffi_shdict_free_space
121
+ end )
122
+ then
123
+ ffi .cdef [[
124
+ size_t ngx_stream_lua_ffi_shdict_free_space (void * zone );
125
+ ]]
126
+ end
127
+
128
+ ngx_lua_ffi_shdict_free_space = C .ngx_stream_lua_ffi_shdict_free_space
129
+
130
+ else
131
+ error (" unknown subsystem: " .. subsystem )
55
132
end
56
133
57
134
if not pcall (function () return C .free end ) then
58
135
ffi .cdef [[
59
- void free (void * ptr );
136
+ void free (void * ptr );
60
137
]]
61
138
end
62
139
@@ -141,11 +218,11 @@ local function shdict_store(zone, op, key, value, exptime, flags)
141
218
return nil , " bad value type"
142
219
end
143
220
144
- local rc = C . ngx_http_lua_ffi_shdict_store (zone , op , key , key_len ,
145
- valtyp , str_val_buf ,
146
- str_val_len , num_val ,
147
- exptime * 1000 , flags , errmsg ,
148
- forcible )
221
+ local rc = ngx_lua_ffi_shdict_store (zone , op , key , key_len ,
222
+ valtyp , str_val_buf ,
223
+ str_val_len , num_val ,
224
+ exptime * 1000 , flags , errmsg ,
225
+ forcible )
149
226
150
227
-- print("rc == ", rc)
151
228
@@ -213,10 +290,10 @@ local function shdict_get(zone, key)
213
290
local value_len = get_size_ptr ()
214
291
value_len [0 ] = size
215
292
216
- local rc = C . ngx_http_lua_ffi_shdict_get (zone , key , key_len , value_type ,
217
- str_value_buf , value_len ,
218
- num_value , user_flags , 0 ,
219
- is_stale , errmsg )
293
+ local rc = ngx_lua_ffi_shdict_get (zone , key , key_len , value_type ,
294
+ str_value_buf , value_len ,
295
+ num_value , user_flags , 0 ,
296
+ is_stale , errmsg )
220
297
if rc ~= 0 then
221
298
if errmsg [0 ] then
222
299
return nil , ffi_str (errmsg [0 ])
@@ -288,10 +365,10 @@ local function shdict_get_stale(zone, key)
288
365
local value_len = get_size_ptr ()
289
366
value_len [0 ] = size
290
367
291
- local rc = C . ngx_http_lua_ffi_shdict_get (zone , key , key_len , value_type ,
292
- str_value_buf , value_len ,
293
- num_value , user_flags , 1 ,
294
- is_stale , errmsg )
368
+ local rc = ngx_lua_ffi_shdict_get (zone , key , key_len , value_type ,
369
+ str_value_buf , value_len ,
370
+ num_value , user_flags , 1 ,
371
+ is_stale , errmsg )
295
372
if rc ~= 0 then
296
373
if errmsg [0 ] then
297
374
return nil , ffi_str (errmsg [0 ])
@@ -394,10 +471,10 @@ local function shdict_incr(zone, key, value, init, init_ttl)
394
471
init_ttl = 0
395
472
end
396
473
397
- local rc = C . ngx_http_lua_ffi_shdict_incr (zone , key , key_len , num_value ,
398
- errmsg , init and 1 or 0 ,
399
- init or 0 , init_ttl * 1000 ,
400
- forcible )
474
+ local rc = ngx_lua_ffi_shdict_incr (zone , key , key_len , num_value ,
475
+ errmsg , init and 1 or 0 ,
476
+ init or 0 , init_ttl * 1000 ,
477
+ forcible )
401
478
if rc ~= 0 then -- ~= NGX_OK
402
479
return nil , ffi_str (errmsg [0 ])
403
480
end
413
490
local function shdict_flush_all (zone )
414
491
zone = check_zone (zone )
415
492
416
- C . ngx_http_lua_ffi_shdict_flush_all (zone )
493
+ ngx_lua_ffi_shdict_flush_all (zone )
417
494
end
418
495
419
496
@@ -437,7 +514,7 @@ local function shdict_ttl(zone, key)
437
514
return nil , " key too long"
438
515
end
439
516
440
- local rc = C . ngx_http_lua_ffi_shdict_get_ttl (zone , key , key_len )
517
+ local rc = ngx_lua_ffi_shdict_get_ttl (zone , key , key_len )
441
518
442
519
if rc == FFI_ERROR then
443
520
return nil , " bad zone"
@@ -475,8 +552,8 @@ local function shdict_expire(zone, key, exptime)
475
552
return nil , " key too long"
476
553
end
477
554
478
- local rc = C . ngx_http_lua_ffi_shdict_set_expire (zone , key , key_len ,
479
- exptime * 1000 )
555
+ local rc = ngx_lua_ffi_shdict_set_expire (zone , key , key_len ,
556
+ exptime * 1000 )
480
557
481
558
if rc == FFI_ERROR then
482
559
return nil , " bad zone"
@@ -495,14 +572,14 @@ end
495
572
local function shdict_capacity (zone )
496
573
zone = check_zone (zone )
497
574
498
- return tonumber (C . ngx_http_lua_ffi_shdict_capacity (zone ))
575
+ return tonumber (ngx_lua_ffi_shdict_capacity (zone ))
499
576
end
500
577
501
578
502
579
local function shdict_free_space (zone )
503
580
zone = check_zone (zone )
504
581
505
- return tonumber (C . ngx_http_lua_ffi_shdict_free_space (zone ))
582
+ return tonumber (ngx_lua_ffi_shdict_free_space (zone ))
506
583
end
507
584
508
585
0 commit comments