http://blog.csdn.net/xing_____/article/details/38457463
- 系统:centos6.4
- redis下载:http://www.redis.cn/download.html
- redis安装步骤:
- 上传源码包到/lamp
- 解压:tar -zxvf redis.tar.gz
- cd redis
- make //编译源码,初装的linux系统到这一步可能会报以下错误
- CC adlist.o
- /bin/sh: cc: command not found
- 就需要装gcc
- yum install gcc
- 安装完后需要删除redis文件,重新解压,因为在原文件make还是会报相同的错误
- rm zxvf redis
- 解压:tar -zxvf redis.tar.gz
- cd redis
- make //编译源码
- cd src
- make install //安装程序
- 为了便于管理可以将redis.conf redis-server redis-cli 这3个放入/usr/local/redis/目录下
- 最后进入redis.conf
- 修改 daemonize no 为daemonize yes #后台运行
- 到此redis 安装完成
6. 启动redis
a) $ cd /usr/local/bin
b) ./redis-server /etc/redis.conf
7. 检查是否启动成功
a) $ ps -ef | grep redis
关闭防火墙 systemctl stop firewalld.service
3. 简单的Redis测试程序
读者可以自行创建Eclipse项目,引入jedis的客户端包,测试程序如下:
- public class RedisTest {
- private Jedis jedis = null;
- private String key1 = "key1";
- private String key2 = "key2";
- public RedisTest() {
- jedis = new Jedis("localhost");
- }
- public static void main(String[] args) {
- RedisTest redisTest = new RedisTest();
- redisTest.isReachable();
- redisTest.testData();
- redisTest.delData();
- redisTest.testExpire();
- }
- public boolean isReachable() {
- boolean isReached = true;
- try {
- jedis.connect();
- jedis.ping();
- // jedis.quit();
- } catch (JedisConnectionException e) {
- e.printStackTrace();
- isReached = false;
- }
- System.out
- .println("The current Redis Server is Reachable:" + isReached);
- return isReached;
- }
- public void testData() {
- jedis.set("key1", "data1");
- System.out.println("Check status of data existing:"
- + jedis.exists(key1));
- System.out.println("Get Data key1:" + jedis.get("key1"));
- long s = jedis.sadd(key2, "data2");
- System.out.println("Add key2 Data:" + jedis.scard(key2)
- + " with status " + s);
- }
- public void delData() {
- long count = jedis.del(key1);
- System.out.println("Get Data Key1 after it is deleted:"
- + jedis.get(key1));
- }
- public void testExpire() {
- long count = jedis.expire(key2, 5);
- try {
- Thread.currentThread().sleep(6000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- if (jedis.exists(key2)) {
- System.out
- .println("Get Key2 in Expire Action:" + jedis.scard(key2));
- } else {
- System.out.println("Key2 is expired with value:"
- + jedis.scard(key2));
- }
- }
- }