java对redis的基本操作


一、server端安装

  1、下载

    https://github.com/MSOpenTech/redis

    可看到当前可下载版本:redis2.6

    

    下载windows平台文件:

    

     

     解压后,选择当前64位win7系统对应的版本:

     

  2、安装

    1)解压后将里面所有文件拷贝至redis安装目录:

     

     几个exe程序的功能:    

        redis-benchmark.exe性能测试,用以模拟同时由N个客户端发送M个 SETs/GETs 查询 (类似于 Apache 的ab 工具).

        redis-check-aof.exe:更新日志检查

       redis-check-dump.exe本地数据库检查

       redis-cli.exe客户端

        redis-server.exe服务端       

    2)将路径添加至系统环境变量:过程略

    3)cmd下启动redis-server

    

    注:由于此处未指定配置文件,系统采用默认参数

  3、下载对应的配置文件

    由上面cmd输出可知,当前版本为2.6.12

    原页面上打开所有发布版本,找到2.6.12:

    

    下载zip文件:

    

    拷贝出redis.conf配置文件至安装目录:

    

    

    配置文件各字段含义:http://cardyn.iteye.com/blog/794194

    ps:如果配置文件的版本和当前exe程序的版本不匹配,很容易

     由于参数不一致导致server端无法启动。

  4、通过指定的配置文件启动服务端

    

  5、客户端访问测试

    另起一个cmd窗口:

    

二、基本功能测试

  1、程序基本结构

  

  2、主要类

    1)功能类

复制代码
package com.redis;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import redis.clients.jedis.SortingParams;



public class RedisClient {

private Jedis jedis;//非切片额客户端连接
private JedisPool jedisPool;//非切片连接池
private ShardedJedis shardedJedis;//切片额客户端连接
private ShardedJedisPool shardedJedisPool;//切片连接池

public RedisClient()
{
initialPool();
initialShardedPool();
shardedJedis
= shardedJedisPool.getResource();
jedis
= jedisPool.getResource();


}

/**
* 初始化非切片池
*/
private void initialPool()
{
// 池基本配置
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(
20);
config.setMaxIdle(
5);
config.setMaxWait(
1000l);
config.setTestOnBorrow(
false);

jedisPool
= new JedisPool(config,"127.0.0.1",6379);
}

/**
* 初始化切片池
*/
private void initialShardedPool()
{
// 池基本配置
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(
20);
config.setMaxIdle(
5);
config.setMaxWait(
1000l);
config.setTestOnBorrow(
false);
// slave链接
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
shards.add(
new JedisShardInfo("127.0.0.1", 6379, "master"));

// 构造池
shardedJedisPool = new ShardedJedisPool(config, shards);
}

public void show() {
KeyOperate();
StringOperate();
ListOperate();
SetOperate();
SortedSetOperate();
HashOperate();
jedisPool.returnResource(jedis);
shardedJedisPool.returnResource(shardedJedis);
}

private void KeyOperate() {
。。。
}

private void StringOperate() {
。。。
}

private void ListOperate() {
。。。
}

private void SetOperate() {
。。。
}

private void SortedSetOperate() {
。。。
}

private void HashOperate() {
。。。
}
}
复制代码

    2)测试类

复制代码
package com.redis;

public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
new RedisClient().show();
}

}
复制代码

  3、各个功能函数

    1)key功能

复制代码
private void KeyOperate() 
{
System.out.println(
"======================key==========================");
// 清空数据
System.out.println("清空库中所有数据:"+jedis.flushDB());
// 判断key否存在
System.out.println("判断key999键是否存在:"+shardedJedis.exists("key999"));
System.out.println(
"新增key001,value001键值对:"+shardedJedis.set("key001", "value001"));
System.out.println(
"判断key001是否存在:"+shardedJedis.exists("key001"));
// 输出系统中所有的key
System.out.println("新增key002,value002键值对:"+shardedJedis.set("key002", "value002"));
System.out.println(
"系统中所有键如下:");
Set
<String> keys = jedis.keys("*");
Iterator
<String> it=keys.iterator() ;
while(it.hasNext()){
String key
= it.next();
System.out.println(key);
}
// 删除某个key,若key不存在,则忽略该命令。
System.out.println("系统中删除key002: "+jedis.del("key002"));
System.out.println(
"判断key002是否存在:"+shardedJedis.exists("key002"));
// 设置 key001的过期时间
System.out.println("设置 key001的过期时间为5秒:"+jedis.expire("key001", 5));
try{
Thread.sleep(
2000);
}
catch (InterruptedException e){
}
// 查看某个key的剩余生存时间,单位【秒】.永久生存或者不存在的都返回-1
System.out.println("查看key001的剩余生存时间:"+jedis.ttl("key001"));
// 移除某个key的生存时间
System.out.println("移除key001的生存时间:"+jedis.persist("key001"));
System.out.println(
"查看key001的剩余生存时间:"+jedis.ttl("key001"));
// 查看key所储存的值的类型
System.out.println("查看key所储存的值的类型:"+jedis.type("key001"));
/*
* 一些其他方法:1、修改键名:jedis.rename("key6", "key0");
* 2、将当前db的key移动到给定的db当中:jedis.move("foo", 1)
*/
}
复制代码

    运行结果:

复制代码
======================key==========================
清空库中所有数据:OK
判断key999键是否存在:false
新增key001,value001键值对:OK
判断key001是否存在:true
新增key002,value002键值对:OK
系统中所有键如下:
key002
key001
系统中删除key002: 1
判断key002是否存在:false
设置 key001的过期时间为5秒:1
查看key001的剩余生存时间:3
移除key001的生存时间:1
查看key001的剩余生存时间:-1
查看key所储存的值的类型:string
复制代码

    2)String功能

复制代码
private void StringOperate() 
{
System.out.println(
"======================String_1==========================");
// 清空数据
System.out.println("清空库中所有数据:"+jedis.flushDB());

System.out.println(
"=============增=============");
jedis.set(
"key001","value001");
jedis.set(
"key002","value002");
jedis.set(
"key003","value003");
System.out.println(
"已新增的3个键值对如下:");
System.out.println(jedis.get(
"key001"));
System.out.println(jedis.get(
"key002"));
System.out.println(jedis.get(
"key003"));

System.out.println(
"=============删=============");
System.out.println(
"删除key003键值对:"+jedis.del("key003"));
System.out.println(
"获取key003键对应的值:"+jedis.get("key003"));

System.out.println(
"=============改=============");
//1、直接覆盖原来的数据
System.out.println("直接覆盖key001原来的数据:"+jedis.set("key001","value001-update"));
System.out.println(
"获取key001对应的新值:"+jedis.get("key001"));
//2、直接覆盖原来的数据
System.out.println("在key002原来值后面追加:"+jedis.append("key002","+appendString"));
System.out.println(
"获取key002对应的新值"+jedis.get("key002"));

System.out.println(
"=============增,删,查(多个)=============");
/**
* mset,mget同时新增,修改,查询多个键值对
* 等价于:
* jedis.set("name","ssss");
* jedis.set("jarorwar","xxxx");
*/
System.out.println(
"一次性新增key201,key202,key203,key204及其对应值:"+jedis.mset("key201","value201",
"key202","value202","key203","value203","key204","value204"));
System.out.println(
"一次性获取key201,key202,key203,key204各自对应的值:"+
jedis.mget(
"key201","key202","key203","key204"));
System.out.println(
"一次性删除key201,key202:"+jedis.del(new String[]{"key201", "key202"}));
System.out.println(
"一次性获取key201,key202,key203,key204各自对应的值:"+
jedis.mget(
"key201","key202","key203","key204"));
System.out.println();


//jedis具备的功能shardedJedis中也可直接使用,下面测试一些前面没用过的方法
System.out.println("======================String_2==========================");
// 清空数据
System.out.println("清空库中所有数据:"+jedis.flushDB());

System.out.println(
"=============新增键值对时防止覆盖原先值=============");
System.out.println(
"原先key301不存在时,新增key301:"+shardedJedis.setnx("key301", "value301"));
System.out.println(
"原先key302不存在时,新增key302:"+shardedJedis.setnx("key302", "value302"));
System.out.println(
"当key302存在时,尝试新增key302:"+shardedJedis.setnx("key302", "value302_new"));
System.out.println(
"获取key301对应的值:"+shardedJedis.get("key301"));
System.out.println(
"获取key302对应的值:"+shardedJedis.get("key302"));

System.out.println(
"=============超过有效期键值对被删除=============");
// 设置key的有效期,并存储数据
System.out.println("新增key303,并指定过期时间为2秒"+shardedJedis.setex("key303", 2, "key303-2second"));
System.out.println(
"获取key303对应的值:"+shardedJedis.get("key303"));
try{
Thread.sleep(
3000);
}
catch (InterruptedException e){
}
System.out.println(
"3秒之后,获取key303对应的值:"+shardedJedis.get("key303"));

System.out.println(
"=============获取原值,更新为新值一步完成=============");
System.out.println(
"key302原值:"+shardedJedis.getSet("key302", "value302-after-getset"));
System.out.println(
"key302新值:"+shardedJedis.get("key302"));

System.out.println(
"=============获取子串=============");
System.out.println(
"获取key302对应值中的子串:"+shardedJedis.getrange("key302", 5, 7));
}
复制代码

      运行结果:

复制代码
======================String_1==========================
清空库中所有数据:OK
=============增=============
已新增的3个键值对如下:
value001
value002
value003
=============删=============
删除key003键值对:1
获取key003键对应的值:null
=============改=============
直接覆盖key001原来的数据:OK
获取key001对应的新值:value001-update
在key002原来值后面追加:21
获取key002对应的新值value002+appendString
=============增,删,查(多个)=============
一次性新增key201,key202,key203,key204及其对应值:OK
一次性获取key201,key202,key203,key204各自对应的值:[value201, value202, value203, value204]
一次性删除key201,key202:2
一次性获取key201,key202,key203,key204各自对应的值:[null, null, value203, value204]

======================String_2==========================
清空库中所有数据:OK
=============新增键值对时防止覆盖原先值=============
原先key301不存在时,新增key301:1
原先key302不存在时,新增key302:1
当key302存在时,尝试新增key302:0
获取key301对应的值:value301
获取key302对应的值:value302
=============超过有效期键值对被删除=============
新增key303,并指定过期时间为2秒OK
获取key303对应的值:key303-2second
3秒之后,获取key303对应的值:null
=============获取原值,更新为新值一步完成=============
key302原值:value302
key302新值:value302-after-getset
=============获取子串=============
获取key302对应值中的子串:302
复制代码

    3)List功能

复制代码
private void ListOperate() 
{
System.out.println(
"======================list==========================");
// 清空数据
System.out.println("清空库中所有数据:"+jedis.flushDB());

System.out.println(
"=============增=============");
shardedJedis.lpush(
"stringlists", "vector");
shardedJedis.lpush(
"stringlists", "ArrayList");
shardedJedis.lpush(
"stringlists", "vector");
shardedJedis.lpush(
"stringlists", "vector");
shardedJedis.lpush(
"stringlists", "LinkedList");
shardedJedis.lpush(
"stringlists", "MapList");
shardedJedis.lpush(
"stringlists", "SerialList");
shardedJedis.lpush(
"stringlists", "HashList");
shardedJedis.lpush(
"numberlists", "3");
shardedJedis.lpush(
"numberlists", "1");
shardedJedis.lpush(
"numberlists", "5");
shardedJedis.lpush(
"numberlists", "2");
System.out.println(
"所有元素-stringlists:"+shardedJedis.lrange("stringlists", 0, -1));
System.out.println(
"所有元素-numberlists:"+shardedJedis.lrange("numberlists", 0, -1));

System.out.println(
"=============删=============");
// 删除列表指定的值 ,第二个参数为删除的个数(有重复时),后add进去的值先被删,类似于出栈
System.out.println("成功删除指定元素个数-stringlists:"+shardedJedis.lrem("stringlists", 2, "vector"));
System.out.println(
"删除指定元素之后-stringlists:"+shardedJedis.lrange("stringlists", 0, -1));
// 删除区间以外的数据
System.out.println("删除下标0-3区间之外的元素:"+shardedJedis.ltrim("stringlists", 0, 3));
System.out.println(
"删除指定区间之外元素后-stringlists:"+shardedJedis.lrange("stringlists", 0, -1));
// 列表元素出栈
System.out.println("出栈元素:"+shardedJedis.lpop("stringlists"));
System.out.println(
"元素出栈后-stringlists:"+shardedJedis.lrange("stringlists", 0, -1));

System.out.println(
"=============改=============");
// 修改列表中指定下标的值
shardedJedis.lset("stringlists", 0, "hello list!");
System.out.println(
"下标为0的值修改后-stringlists:"+shardedJedis.lrange("stringlists", 0, -1));
System.out.println(
"=============查=============");
// 数组长度
System.out.println("长度-stringlists:"+shardedJedis.llen("stringlists"));
System.out.println(
"长度-numberlists:"+shardedJedis.llen("numberlists"));
// 排序
/*
* list中存字符串时必须指定参数为alpha,如果不使用SortingParams,而是直接使用sort("list"),
* 会出现"ERR One or more scores can't be converted into double"
*/
SortingParams sortingParameters
= new SortingParams();
sortingParameters.alpha();
sortingParameters.limit(
0, 3);
System.out.println(
"返回排序后的结果-stringlists:"+shardedJedis.sort("stringlists",sortingParameters));
System.out.println(
"返回排序后的结果-numberlists:"+shardedJedis.sort("numberlists"));
// 子串: start为元素下标,end也为元素下标;-1代表倒数一个元素,-2代表倒数第二个元素
System.out.println("子串-第二个开始到结束:"+shardedJedis.lrange("stringlists", 1, -1));
// 获取列表指定下标的值
System.out.println("获取下标为2的元素:"+shardedJedis.lindex("stringlists", 2)+"\n");
}
复制代码

      运行结果:

复制代码
======================list==========================
清空库中所有数据:OK
=============增=============
所有元素-stringlists:[HashList, SerialList, MapList, LinkedList, vector, vector, ArrayList, vector]
所有元素-numberlists:[2, 5, 1, 3]
=============删=============
成功删除指定元素个数-stringlists:2
删除指定元素之后-stringlists:[HashList, SerialList, MapList, LinkedList, ArrayList, vector]
删除下标0-3区间之外的元素:OK
删除指定区间之外元素后-stringlists:[HashList, SerialList, MapList, LinkedList]
出栈元素:HashList
元素出栈后-stringlists:[SerialList, MapList, LinkedList]
=============改=============
下标为0的值修改后-stringlists:[hello list!, MapList, LinkedList]
=============查=============
长度-stringlists:3
长度-numberlists:4
返回排序后的结果-stringlists:[LinkedList, MapList, hello list!]
返回排序后的结果-numberlists:[1, 2, 3, 5]
子串-第二个开始到结束:[MapList, LinkedList]
获取下标为2的元素:
本站声明
本文转载自:http://avords.iteye.com/blog/2242690     作者:avords     发布日期:2015-09-11     本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。


 
© 2014-2016 ITdaan.com 粤ICP备14056181号