Using Redis to store word occurences I am doing a project where I am analyzing a group of Tweets. 

What I need to do is find the occurrence of each individual word. I am fairly new to Redis, so I am not quite sure that my solution is optimal. I am storing a key value pair for each word. The key is a word, and the value is the occurrence count. I then use a list to keep track of every word that goes into the cache.

$cache = new Redis;
$words = array('foo', 'bar', 'baz');

foreach ($words as $word) {
    if ($cache-&gt;exists($word) {
        $cache-&gt;incr($word);
    } else {
        $cache-&gt;put($word, 1);
        $cache-&gt;lpush('words', $word);
    }
}

$data = array();
$list = $cache-&gt;lrange('words', 0, -1);

foreach ($list as $word) {
    $data[$word] = $cache-&gt;get($word);
}

asort($data);
Your code is alright, really. However, by your using redis, I suspect you're after a fast way to store tiny bits of data. The varname $cache somewhat confirms my suspicion.
If  this is the case, your using lrange might not be the best choice, nor is exists really meaningful: INCR creates a new key, if required, and it returns the new value of that key in every case, so you can ditch the use of put, and simplify your first loop to:

foreach ($words as $word)
{
    if ($cache-&gt;incr($word) == 1)
        $cache-&gt;lpush('words', $word);
}


Both these functions are atomic, so I don't think you can much improve on that.

lrange has a time-complexity of O(S+N) according to the documentation. Given that you're just fetching the entire words collection, I'd opt for the O(n) alternative SMEMBERS.
If you are going to use lrange, if you want to use pagination, for exampe, I'd consider using SCARD first, to determine the total number of members of your words collection.

Anyway, here's how I'd write the second loop:

$list = $cache-&gt;smembers('words');
foreach ($list as $word)
{
    $data[$word] = $cache-&gt;get($word);
}


I'll look into using redis itself a bit more, because it's quite easy to do away with that words collection, too:

foreach ($words as $word)
    $cache-&gt;incr($word);
$data = array();
$list = $cache-&gt;keys('*');//get all keys in collection
foreach ($list as $word)
    $data[$word] = $cache-&gt;get($word);


But this would imply the entire database contains nothing but words, which isn't what you want, probably. If I find the time, I'll attempt to put together a function or script that simply iterates over that words collection, and performs something like this

EVAL "return {KEYS[1], redis.call('get', KEYS[1])}" 1 &lt;a word&gt;


for each word, returning a series of redis objects that look like this:

1) &lt;a word&gt;
2) 123


where the 2) actually is the occurance count

Unrelated
I notice you're constructing an object of the Redis class. This, to me, tells me you're using a library other than the official one. The PRedis class was, last time I checked, the preferred one. Perhaps consider changing to that lib, and see if it works for you. Anyway, look into the libs with a star next to their name :)