openresty / lua-resty-memcached

Lua memcached client driver for the ngx_lua based on the cosocket API
211 stars 63 forks source link

How to use prefix with ":" in key #16

Closed igarbera closed 7 years ago

igarbera commented 7 years ago

Hello! I'm new in lua, and have an issue: I have data in memcached, putted by PHP with prefix with colon, such as 'appname:' . Key example is 'appname:08d1effdff9b03e8f54e55a72c703471'

So, with lua I need get data from memcached, but memc:get('appname:08d1effdff9b03e8f54e55a72c70347') not works (return nil). But if key is without colon, as memc:get('08d1effdff9b03e8f54e55a72c703471') , it works fine.

How can I use keys with prefix like 'appname:value' in lua?

agentzh commented 7 years ago

@igarbera Just specify your own key_transform function:

https://github.com/openresty/lua-resty-memcached#new

igarbera commented 7 years ago

Excuse me, could you show an example for the keys like prefix:value?

agentzh commented 7 years ago

@igarbera Read the documentation. The keys are escaped to the URI encoding by default. Just use an identity function by setting the key_transform option value when doing the new() call.

igarbera commented 7 years ago

So, the problem is, I do not quite understand what parameters for key_transform should be set in order to use keys like 'prefix: value'. Sorry for the stupid questions :(

agentzh commented 7 years ago
    local function identity(a) return a end

    local memc = memached:new{
        key_transform = { identity, identity }
    }
igarbera commented 7 years ago

Thank you wery much, now understand! )