Redis安装及简单测试

Redis安装与基础测试教程

摘要: Redis是目前业界非常受到欢迎的一个内存数据库,一般用作系统的中间缓存系统,用以提升整体商业系统的吞吐量和响应速度。本文将简要介绍安装的主要过程以及给出一个简要的测试代码。


1.  系统环境和版本说明

   操作系统选用Ubuntu 14.04, Redis的版本选取目前的最新稳定版本2.8.9. 客户端选用了Redis的Java版本jedis 2.4.2.

2.  Redis的安装步骤

   a. 下载Redis的安装包

      wget http://download.redis.io/releases/redis-2.8.9.tar.gz
   b. 在目录下,解压按照包,生成新的目录redis-2.8.9

      tar xvfz redis-2.8.9.tar.gz

    c.  进入解压之后的目录,进行编译

        cd redis-2.8.9

        sudo make

       

        说明: 如果没有明显的错误,则表示编译成功

     d.  安装

        sudo make install

         说明: 一般情况下,在Ubuntu系统中,都是需要使用sudo提升权限的

      e.   在安装成功之后,可以运行测试,确认Redis的功能是否正常

         sudo make test

        

    f.  启动Redis服务

       查找Redis安装的目录:  find /  -name 'redis*'  ------ 在根目录下查找名称中含有redis的文件

      经过查找,发现Redis被安装到了/usr/local/bin/目录下。

      接下来,启动Redis服务:

          /usr/local/bin/redis-server

       

     说明: 从以上的截图中,可以发现启动的端口为缺省的6379. 用户可以在启动的时候,指定具体的配置文件,并在其中指定启动的端口。

  g.  查看Redis进程

        ps -ef | grep redis

      

说明: 如果可以看到进程,说明启动正常。


3.   简单的Redis测试程序

    读者可以自行创建Eclipse项目,引入jedis的客户端包,测试程序如下:

    

  1. public class RedisTest { 
  2.  
  3.     private Jedis jedis = null
  4.     private String key1 = "key1"
  5.     private String key2 = "key2"
  6.  
  7.     public RedisTest() { 
  8.         jedis = new Jedis("localhost"); 
  9.     } 
  10.  
  11.     public static void main(String[] args) { 
  12.         RedisTest redisTest = new RedisTest(); 
  13.         redisTest.isReachable(); 
  14.         redisTest.testData(); 
  15.         redisTest.delData(); 
  16.         redisTest.testExpire(); 
  17.     } 
  18.  
  19.     public boolean isReachable() { 
  20.         boolean isReached = true
  21.         try
  22.             jedis.connect(); 
  23.             jedis.ping(); 
  24.             // jedis.quit(); 
  25.         } catch (JedisConnectionException e) { 
  26.             e.printStackTrace(); 
  27.             isReached = false
  28.         } 
  29.  
  30.         System.out 
  31.                 .println("The current Redis Server is Reachable:" + isReached); 
  32.         return isReached; 
  33.     } 
  34.  
  35.     public void testData() { 
  36.         jedis.set("key1", "data1"); 
  37.  
  38.         System.out.println("Check status of data existing:" 
  39.                 + jedis.exists(key1)); 
  40.         System.out.println("Get Data key1:" + jedis.get("key1")); 
  41.  
  42.         long s = jedis.sadd(key2, "data2"); 
  43.         System.out.println("Add key2 Data:" + jedis.scard(key2) 
  44.                 + " with status " + s); 
  45.     } 
  46.  
  47.     public void delData() { 
  48.         long count = jedis.del(key1); 
  49.  
  50.         System.out.println("Get Data Key1 after it is deleted:" 
  51.                 + jedis.get(key1)); 
  52.     } 
  53.  
  54.     public void testExpire() { 
  55.         long count = jedis.expire(key2, 5); 
  56.  
  57.         try
  58.             Thread.currentThread().sleep(6000); 
  59.         } catch (InterruptedException e) {            
  60.             e.printStackTrace(); 
  61.         } 
  62.  
  63.         if (jedis.exists(key2)) { 
  64.             System.out 
  65.                     .println("Get Key2 in Expire Action:" + jedis.scard(key2)); 
  66.         } else
  67.             System.out.println("Key2 is expired with value:" 
  68.                     + jedis.scard(key2)); 
  69.         } 
  70.     } 
  71.  
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));
		}
	}

}

4. 总结

   本文简要直观介绍了Redis的安装和部署,并基于jedis的简单测试程序,说明了Redis的基本使用情况,更多的内容,可以查阅相关资料。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值