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. TheyThe 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->exists($word) {
$cache->incr($word);
} else {
$cache->put($word, 1);
$cache->lpush('words', $word);
}
}
$data = array();
$list = $cache->lrange('words', 0, -1);
foreach ($list as $word) {
$data[$word] = $cache->get($word);
}
asort($data);