The scripting capabilities of cacheismo are very powerful. The code for handing commands like get/set/add etc is completely written in lua using the lua interface. Here is the code for handling SET command:
local function handleSET(command)
local hashMap = getHashMap()
local cacheItem = hashMap:get(command:getKey())
if (cacheItem ~= nil) then
cacheItem = nil
cacheItem = command:newCacheItem()
if (cacheItem ~= nil) then
command:writeString("SERVER_ERROR Not Enough Memory\r\n")
This is the list of the lua methods which can be called by new scripts
- Global method, return instance of the global hashmap object.
- Sets the logging level. Four levels are defined DEBUG=0, INFO=2,
WARN=2 and ERR=3. The function takes numeric argument.
The standard table object in lua is extended to support the following methods
- returns a string object which represents the serialized table.
- returns a fully constructed table from the given serialized table string
- returns a deep copy of the given table
The global hashmap object retrieved from getHashMap() supports following methods
- returns a cacheItem object for the given key if found, else nil
- takes a cacheItem object and puts it in the hashMap
- deletes the cacheItem object associated with the given key from the hashMap.
- deletes as many LRU objects as required to free up at least requiredSpace.
The data in the hashMap is stored in the form of cacheItem objects. We need to have
a cacheItem object to store in the map and cacheItem is what we get from the map
when we do a get. The object supports following methods. These objects are
- returns the key associated with the cacheItem
- returns the number of bytes in the key
- returns the time in seconds since epoch when the current item will expire.
- returns the flags associated with the cacheItem
- returns the size of the data stored in this cacheItem
- returns the data stored in the cacheItem as lua string. This is normally not
used because lua script don't need access to actual data unless using virtual keys.
- deletes the reference to the cacheItem object. Important to call this after get
from hashMap, after using the cacheItem.
This represents the request coming from the client. This can be modified if
required by the script.
- returns the memcached the command being used.
One of get, add, set, replace, prepend, append, cas, incr, decr, gets, delete,
stats, flush_all, version, quit or verbosity
- returns the key used in the command. This is nil in case of multi-get and other
commands which don't have key as argument.
- set the given string newKey as the key for the command object.
- returns the size in bytes for the key
- returns the expiryTime specified in the command.
- sets the expiry time to new value newTime.
- returns the flags specified in the command. It is also used by the verbosity
command to return the logging level.
- sets the falgs in the command object to new flags.
- returns the delta value used in incr/decr commands
- sets the delta value to new delta value
- returns the boolean noReply from the command.
- sets the value for the noReply
- returns the size of data in the command, in bytes. This works for set, add, etc.
- replaces the data in the command object with the newData string
- returns the data in the command object as lua string
- creates and returns a cacheItem object using the data in the command object.
This is used with set/add/etc
- writes the cacheItem in the memcached response format on the client socket.
"VALUE key flags size\r\ndata\r\n"
cas is not supported.
- writes arbitrary string to the client socket. This is useful for writing
"END\r\n", "STORED" and other standard memcached response strings.
- returns the number of multi-get keys in the command
- returns a table of all the keys in the multi-get command
Apart from these, few helper methods defined in config.lua are also useful.
writeStringAsValue(command, key, value)
- writes an arbitrary string in the "VALUE key flags size\r\ndata\r\n" format.
flags is 0 and size is calculated using string.len
executeReadOnly(command, originalKey, objectType, cacheKey, func, ...)
- helper function for read only operations on lua tables stored in the cache.
It retrievs the data from the cache, uses table.unmarshal to construct the
lua table and calls the function func with this lua table as the first
argument. Any extra args passed to this function are passed to function func.
executeReadWrite(command, originalKey, objectType, cacheKey, func, ...)
- helper function for read/write operations on lua tables stored in the cache
orginal object is deleted and a new object is created with the same key with
latest data values
executeNew(command, originalKey, objectType, cacheKey, func, ...)
- helper function for creation of new objects based on lua tables. if the key
is in use, it is deleted before creating the new object
See set.lua, map.lua, quota.lua and swcounter.lua for example usage.