对redis的java的客户端进行API使用的简单介绍

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值