qileilove

blog已经转移至github,大家请访问 http://qaseven.github.io/

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的客户端包,测试程序如下:
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));
}
}
}
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的基本使用情况,更多的内容,可以查阅相关资料。

posted on 2014-06-03 09:59 顺其自然EVO 阅读(569) 评论(0)  编辑  收藏 所属分类: 测试学习专栏


只有注册用户登录后才能发表评论。


网站导航:
 
<2014年6月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

导航

统计

常用链接

留言簿(55)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜