Suppose you have news website and you want to show last 20 news on the homepage.
Some log files works in same way.
Using MySQL this can be done with "select - order by - limit" query. If there are lots of news, the table will eighter grow or will became fragmented. In both cases site will slow down dramaticly.
Here is how this may be done with Redis.
$r = new Redis(); $r->connect("127.0.0.1", "6379"); $newsroom = "sport"; $newsitem = "Barcelona vs Sporting Lissabon 2:1"; // Create a hash key $key = "news:" . $newsroom; // Push into the list... $r->lpush($key, $newsitem); // Trim the list, so it can contain up to 20 items... $r->ltrim($key, 0, 20 - 1);
Redis connection note
The example uses generic connection to the Redis server. To make the example work with our service you will need to use code similar to this one:
// change these parameter according to the information in your instance list $host = '22.214.171.124'; $port = 1234; $password = 'somehashcode'; $db = 0; $r = new Redis(); $r->connect($host, $port); $r->auth($password); $r->select($db);
blog comments powered by Disqus
Because we want this page to be useful for memcached users, we tagged with memcached all examples that may be "recreated" for memcached server.
How to use Redis for cache MySQL queries in a way similar to memcached
How to use Redis to prevent web-scraping parsing and web-spam
How to do 'Rotating' news list
How pool random element from news list
How Redis can help us with Accounting / Vote / Like / Recommend / +1 clicks
How to implement fast hi-score table
How to scale Redis on several servers using sharding
How to implement distributed work pool and to scale some work across many computers
How to collect and store information for page visitors
How to make set members to expire
How Redis can help us with user level locks
Creating distributed message queue
Building reverse index using sets
Using Redis to speed up MySQL inserts
Using Redis for caching MySQL or Cassandra data inserts
How to produce 'There are 123 visitors on this page' sign