yikai.shao Backend Dev Engineer

Redis入门

2017-04-21

redis-server安装
sudo apt-get install redis-server
启动redis服务
redis-server

连接redis服务,类似mysql

redis-cli -h host -p port -a password

基本命令

SET username Tom
GET username

PHP连接Redis

假如php没有安装redis扩展,需要先安装。

sudo apt-get install php5-redis

安装后重启web服务器(apache或php-fpm)

测试连接Redis

<?php
	$redis = new Redis();
	$redis->connect('127.0.0.1', 6379);
	echo "Redis server is running:" . $redis->ping();
?>

Redis数据类型

strings

Binary-safe strings

lists

[队列] collections of string elements sorted according to the order of insertion. They are basically linked lists.

sets

[集合] collections of unique, unsorted string elements.

zsets

[有序集合] similar to Sets but where every string element is associated to a floating number value, called score. The elements are always taken sorted by their score, so unlike Sets it is possible to retrieve a range of elements (for example you may ask: give me the top 10, or the bottom 10).

hashes

[哈希表] which are maps composed of fields associated with values. Both the field and the value are strings. This is very similar to Ruby or Python hashes.

Bit arrays (or simply bitmaps)

it is possible, using special commands, to handle String values like an array of bits: you can set and clear individual bits, count all the bits set to 1, find the first set or unset bit, and so forth.

HyperLogLogs

this is a probabilistic data structure which is used in order to estimate the cardinality of a set. Don’t be scared, it is simpler than it seems… See later in the HyperLogLog section of this tutorial.


概要