Skip to content

Redis#


Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache, and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes with radius queries, and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions, and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster. Learn More…

Redis Applications#

This example opens a Redis connection and keeps track of how many times a user has viewed your website.

Unique visitor counter
<MvIF EXPR="{ NOT redis_connect( 'localhost', '6379', l.conn_id ) }">
    <MvEVAL EXPR="{ 'Failed to connect to redis: ' $ redis_last_error() }">
    <MvEXIT>
</MvIF>

<MvASSIGN NAME="l.null" VALUE="{ redis_incr( l.conn_id, 'unique_counter_' $ s.remote_addr ) }">

<MvEVAL EXPR="{ 'You have visited my site ' $ redis_get( l.conn_id, 'unique_counter_' $ s.remote_addr ) $ ' times' }">

<MvASSIGN NAME="l.null" VALUE="{ redis_disconnect( l.conn_id ) }">

In this example, we cache frequently accessed data for three days. Assume you have a page that performs costly operations and takes more than three seconds to load. This page will be viewed by multiple users, but the data doesn’t change often.

Caching expensive data
<mvt:assign name="l.data" value="redis_get( l.conn_id, 'some_storage_key' )" />

<mvt:if expr="NOT ISNULL l.data">
    <!-- The key data still exists so serve up the cached version -->
    <mvt:eval expr="l.data" />
<mvt:else>
    <!-- The key data does not exist so build it all again -->
    <!-- some very costly operations are performed here and the result is stored in l.costly_calculated_storage -->
    <!-- The data is stored in Redis for 60 * 60 * 24 * 3 seconds (which is 3 days) and then Redis will auto purge it. -->
    <mvt:assign name="l.null" value="redis_set( l.conn_id, 'some_storage_key,' l.costly_calculated_storage, miva_array_deserialize(':EX=' $ 60*60*24*3 ') )" />
</mvt:if>

Redis Functions#

The following are implementations of Redis functions. All functions take a Redis connection reference and work as described by redis.io.