java客户端:jedis
//创建maven依赖,pox.xml中设置
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependencies>
//java使用直连形式访问redis
import redis.clients.jedis.Jedis;
/**
* @author: ltj
* 对redis进行直连
* date: 2019-03-12-0:18
**/
public class demo {
public static void main(String[] arg){
//创建jedis对象,host:IP地址,post:端口,connectTimeOut:连接超时时间,timeOut:客户端读写超时
Jedis jedis = new Jedis(host,post,connectTimeOut,timeOut);
/*字符串演示
* 分别演示设置、演示、计算值长度
* */
jedis.set("name","jie");
jedis.get("name");
jedis.strlen("name");
/*hash演示
* 分别演示设置、获取、删除
* */
jedis.hset("hKey","name","jie");
jedis.hget("hKey","name");
jedis.hdel("hKey","name");
/*list演示
* 分别演示推进,弹出,移除
*/
jedis.lpush("lKey","A","B","C");
jedis.lpop("lKey");
jedis.lrem("lKey",1,"B");
/**set(无序集合)
* 分别演示增加、获取所有的元素、移除集合中某个元素
*/
jedis.sadd("setKey","it","basketball","computer");
jedis.smembers("setKey");
jedis.srem("setKey","it");
/**zset(有序集合)
* 分别演示元素添加、元素分数自增、元素移除
*/
jedis.zadd("zsetKey",99,"jie");
jedis.zincrby("zsetKey",1,"jie");
jedis.zrem("zsetKey","jie");
}S
}
//java使用连接池形式访问redis
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
/**
* @author: ltj
* 使用连接池的形式访问redis
* date: 2019-03-12-23:55
**/
public class jedisPool {
public static void main(String[] args){
//初始化连接池
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
//创建jedis连接池
JedisPool pool = new JedisPool(config,host,port);
Jedis jedis = null;
try {
//从jedisPool中获取资源
jedis = pool.getResource();
//对redis执行操作
jedis.set("hello","world");
System.out.print(jedis.get("hello"));
}catch (Exception exception){
System.out.print(exception);
}finally {
if (jedis!=null){
//将redis放回jedisPool中,而不是关闭
jedis.close();
}
}
}
}
部分功能为进行演示,方法可参考该地址的方法,通过对象引用redis中的方法
https://blog.csdn.net/weixin_42739916/article/details/88089193