Redis Library
The Redis library for MicroMacro can be used to interface with a Redis server. Please review the Redis documentation to learn more. #Loading the Redis Library

The Redis library is optional. In order to use this library, you must require it in one of your source files (preferably main.lua) prior to use. It only needs to be required once in a single file. It is recommended you do this at the top of your main.lua file, outside of any functions or other code, and you may do this by using this code:

Example:
require 'redis/redis';

#Supported Methods #Constructor class Redis()

Instantiates a new Redis client object. The constructor has no side effects and accepts no parameters.

You must call the connect() method to initiate a connection to a Redis server.

Example
require 'redis/redis'; redis = Redis();
#Redis:connect class Redis:connect() class Redis:connect(string host) class Redis:connect(string host, int port) class Redis:connect(string host, int port, string username, string password)

Connects to a remote Redis server. All parameters are optional. If you do not specify a hostname or port, defaults "127.0.0.1" and 6379 will be used.

Example:
local redis = Redis() redis:connect('192.168.1.5') -- Connect to the Redis server at 192.168.1.5
#Redis:close Redis:close()

Closes the connection to the Redis server; this will completely destroy and free the socket.

Example:
local redis = Redis() redis:connect('192.168.1.5') -- Connect to the Redis server at 192.168.1.5 -- ... do something here redis:close()
#Redis:send string Redis:send(string command)

Sends a command to the remote Redis server. The result of the operation will be returned.

Example:
local response = redis:send('PING hello world') print('Response:', response) -- Redis will PONG with the same message we passed in, so should print "hello world" redis:send('set my-test-key 100') -- Set my-int-test-key to 100 local myIntTestKey = redis:send('get my-test-key') -- Set myIntTestKey to the value read from Redis -- should be 100 redis:send('set my-test-key "strings work too"') -- Set my-int-test-key to strings work too redis:send('set key-one 1') redis:send('set key-two 2') redis:send('set key-three 3') local keys = redis:send('keys key-*') -- keys now contains {"key-one", "key-two", "key-tree"}
#Redis:get string|nil Redis:get(string key)

Retrieve the value for a key stored in Redis. Returns nil if the key does not exist.

Redis:get() should only be used with key-value pairs and not against a list (see: Redis:push()).

Example:
redis:set('my-key', 1) local myKey = redis:get('my-key')
#Redis:set string|nil Redis:set(string key, value) string|nil Redis:set(string key, value, number expiry)

Sets a value in Redis with optional expiry. If expiry is not given, then the key will persist until the delete command is called. If expiry is given, it should be in seconds. Floating-point numbers (for example, 1.5) are acceptable.

If everything went well, either "OK" or the previous value will be returned. If there was an error, then nil will be returned. See Redis set command documentation for more detail.

Example:
redis:set('my-key', 123) print(redis:get('my-key')) -- will print 123 redis:set('my-key', 123, 1.5) system.rest(2000) print(redis:get('my-key')) -- will print nil, since the key expired
#Redis:delete number Redis:delete(string key)

Removes a key from the Redis store. The number of items removed will be returned.

Example:
redis:delete('my-key')
#Redis:keys table|string Redis:keys(string key, string pattern)

Return a list of keys that match a given pattern. This function can be relatively slow for larger databases and it is recommended that you only use this for debugging.

See also: Redis:scan()

#Redis:scan table Redis:scan() table Redis:scan(string pattern)

Iterates over the database and returns keys that match a given pattern. If no pattern is given, then all available keys should be returned. You may use "*" as a wildcard character.

Example:
redis:scan('my-') -- Return all keys that begin with "my-" redis:scan('*-*') -- Return all keys that contain a "-"
#Redis:push table|string Redis:push(string key, ...)

Adds one or more items to the end of the list for the given key. Any given items will preserve the order they were pushed in. That is, if you push the values "a", "b" and "c", then push "d", "e" and "f", the full list will contain "a", "b", "c", "d", "e", "f".

If the list with the given key does not exist, one will be created for you.

Example:
redis:push('my-list', "hello") -- Create and push "hello" into my-list redis:push('my-list', "world") -- Push "world" onto the end of my-list
#Redis:pushFront table|string Redis:pushFront(string key, ...)

Exactly the same as Redis:push(), except that this adds the items to the front of the list.

Items will retain the order they were pushed. For example if you push "a", "b" and "c", then push "1", "2" and "3", the full list will be "1", "2", "3", "a", "b", "c"

Example:
redis:pushFront('my-list', "world") redis:pushFront('my-list', "hello")
#Redis:pop table|string Redis:pop(string key) table|string Redis:pop(string key, number count)

Removes items from the end of the list. If `count` is given, then that many items will be removed, otherwise only the last item will be removed.

Removed items will also be returned.

Example:
redis:pop('my-list')
#Redis:popFront table|string Redis:popFront(string key) table|string Redis:popFront(string key, number count)

Exactly like Redis:pop(), except that it removes items from the front of the list.

#Redis:slice table Redis:slice(string key) table Redis:slice(string key, number from, number to)

Can be used to retrieve items from within a list. If neither `from` nor `to` are given, the full list will be returned. If only `from` is given, then items beginning on that index until the end of the list will be returned.

The indexes are 0-based. That means that the first item is index 0, the second item is index 1, and so on. Therefore, a value of 1 for `from` will result in returning items beginning on the second item.

You may pass negative numbers to specify values relative to the end of the list. -1 would be the last item in the list, -2 the second to last, and so on. For example, to return only the last 3 items, you could pass -3 for `from`.

Example:
redis:push('my-list', 'a', 'b', 'c', 'd', 'e', 'f', 'g') table.print(redis:slice('my-list', 2, -2)) -- Will print a table containing "c", "d", "e", "f"
#Redis:increment number Redis:increment(string key) number Redis:increment(string key, number amount)

Increments a value by the given amount. If the `amount` isn't given, a value of 1 will be assumed. If the key does not already exist, it will be assumed to have been 0.

Example:
redis:set('my-key', 1) -- Set to 1 redis:increment('my-key') -- my-key should now be 2 redis:increment('my-key', 2) -- my-key should now be 4 print("my-key:", redis:get('my-key')) -- Should print 4
#Redis:decrement number Redis:decrement(string key) number Redis:decrement(string key, number amount)

Exactly the same as Redis:increment(), except that this will subtract rather than add.

Page last updated at 2021-11-01 18:59:26


Copyright 2024