Upfront, I'll say this may be a terrible idea :)
I've been reading though issues/PR's and it seems that extending the shared dict is a common theme. Currently, we must do all of this experimentation in C and you must recompile, etc, if you do patch the source.
The basic pattern inside the shdict code is:
- lock the dictionary
- do an operation, on entire dict or an individual item
- unlock the dictionary
My idea was to allow dev to do the second step in Lua. This would probably be slower as it would call back into Lua, but for experimentation, this is probably fine. For many real world use cases, this may be fine, as well.
An API may look something like:
ngx.shared.<name>.callback(key, func)
where func is a function which takes a single arguments which is a shared dictionary item with the fields:
key
flags
expire time
value
We could have some helper function for manipulating these - or just check if the values where changed by the call back.
We would return values similar to pcall - an initial "ok" value and then any returns from the callback function. On failure, false, then an error.
Initially, I'd just focus on individual key/value pairs as it is a bit more straightforward.
Thoughts?