栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

IDEA配置Redis及简单运用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

IDEA配置Redis及简单运用

IDEA配置Redis及简单运用
    • 前提:已安装Redis,导入jedis.jar,commons-pool2.jar
    • 配置连接池RedisUtils.java
    • RedisJDBCTest.java
    • jedis.properties

前提:已安装Redis,导入jedis.jar,commons-pool2.jar

            redis.clients
            jedis
            2.8.0
        
        
            org.apache.commons
            commons-pool2
            2.4.2
        
配置连接池RedisUtils.java
public class RedisUtils {
    private static JedisPool jedisPool;
    static {
        //创建properties文件
        Properties properties = new Properties();
        //读取配置文件
        InputStream is= RedisUtils.class.getResourceAsStream("jedis.properties");
       //加载配置文件
        try {
            properties.load(is);
        }catch (IOException e){
            e.printStackTrace();
        }
        //System.out.println(properties.getProperty("host"));
        //创建连接池对象
        JedisPoolConfig config = new JedisPoolConfig();
        //最大允许连接数
        config.setMaxTotal(Integer.parseInt(properties.getProperty("maxTotal")));
        //最大空闲连接数
        config.setMaxIdle(Integer.parseInt(properties.getProperty("maxIdle")));
        //创建jedisPool连接池对象 timeout客户端超时时间
        jedisPool = new JedisPool(config,properties.getProperty("host"),Integer.parseInt(properties.getProperty("port")),3000,properties.getProperty("password"));
       
    }
    //返回数据连接池
    public static JedisPool jedisPool(){
        return jedisPool;
    }
    //返回Jedis连接
    public static Jedis getJedis(){
        return jedisPool.getResource();
    }
    //关闭资源
    public static void getClose(Jedis jedis){
        if(jedis!=null){
            jedis.close();
        }
    }
    public static void getClose(JedisPool jedisPool){
        if(jedisPool!=null){
            jedisPool.close();
        }
    }
}

RedisJDBCTest.java
public class RedisJDBCTest {
    public static void main(String[] args) {
        //建立一个连接
        Jedis jedis = RedisUtils.getJedis();
      //操作list
        for (int i=1;i<6;i++){
            jedis.rpush("sum",i+"");//右边开始添加
           //jedis.lpush()//左边开始添加
        }
        jedis.lpop("sum");//删除并返回左边第一个元素
        jedis.rpop("sum");//删除并返回右边第一个元素
        jedis.lrem("sum",0,"2");//count=0删除全部value为2,count>0从头开始删除value=2数量为count,count<0从尾开始开始删除value=2数量为count
        System.out.println(jedis.lrange("sum",0,5));//取第1~6值
        System.out.println(jedis.llen("sum"));//长度
     // set
        jedis.sadd("person","学生");
        jedis.sadd("person","艺术","美术");
        System.out.println(jedis.smembers("person"));
     //hash
         jedis.hset("hash","k1","v1");
         jedis.hset("hash","k2","v2");
        //删除键值对
        jedis.hdel("hash","k1");
        //取出所有value,返回list
        System.out.println(jedis.hvals("hash"));
        //取出指定key的value
        System.out.println(jedis.hget("hash","k2"));
        //取出所有key-value,map类型
        System.out.println(jedis.hgetAll("hash"));
        //jedis.del("name");
        //关闭资源
        RedisUtils.getClose(jedis);
    }
}

jedis.properties
host=127.0.0.1
port=6379
password=123456
maxTotal=20
maxIdle=10
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/683011.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号