how to call LUA per inserted row

4 views
Skip to first unread message

Jak Sprats

unread,
Jan 14, 2011, 4:24:32 PM1/14/11
to redisql-dev
Hi All,

I needed to put a per FK cap on a table .... (e.g. I needed to limit
the number of entries a user could have in a table to 100)

In this example, "thread" is the table, and "page_no" is the foreign-
key, so a single "page_no" is only allowed to have 100 entries, if he
has 101, his first entry will be deleted ....

sounds kinda tough to do in a relational database, but this is
AlchemyDB, it is doable in 2 lines.

I used AlchemyDB insert-triggers, LUA, and redis-lists ... which all
play nicely w/ each other.
Each FK has its own redis-list, which works as a Queue, when the list
reaches 100, its first value is RPOPed and the value is a PK in the
table, and this PK gets SQL-DELETEd :)
All of this logic is done in LUA, on every INSERT.

Here is the gist:
https://gist.github.com/780217

On a 2.8GHZ CPU, 11K INSERT/s

Thought this might be useful as an illustration of how to combine
insert-triggers w/ lua to do some trivial logic datastore-side. This
approach can even be used to cache data.

- Jak
Reply all
Reply to author
Forward
0 new messages