• Jedis操作Redis数据库


    关于NoSQL的介绍不写了,直接上代码

    第一步导包,不多讲

    基本操作:

    package demo;
    
    import org.junit.Test;
    
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    import redis.clients.jedis.JedisPoolConfig;
    
    public class Demo {
        // 通过Java程序访问Redis数据库
        @Test
        public void test1() {
            // 获得连接对象
            Jedis jedis = new Jedis("localhost", 6379);
    
            // 存储、获得数据
            jedis.set("username", "yiqing");
            String username = jedis.get("username");
            System.out.println(username);
        }
    
        // Jedis连接池获得jedis连接对象
        @Test
        public void test2() {
            // 配置并创建redis连接池
            JedisPoolConfig poolconfig = new JedisPoolConfig();
            // 最大(小)闲置个数
            poolconfig.setMaxIdle(30);
            poolconfig.setMinIdle(10);
            // 最大连接数
            poolconfig.setMaxTotal(50);
    
            JedisPool pool = new JedisPool(poolconfig, "localhost", 6379);
    
            // 获取资源
            Jedis jedis = pool.getResource();
            jedis.set("username", "yiqing");
            String username = jedis.get("username");
            System.out.println(username);
    
            // 关闭资源
            jedis.close();
            // 开发中不会关闭连接池
            // pool.close();
        }
    }

    注意:如果运行失败,那么原因只有一条:没有打开Redis:

    好的,我们可以用可视化工具观察下:

    保存成功!!

    接下来:

    我们需要抽取一个工具类,方便操作:

    package demo;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    import redis.clients.jedis.JedisPoolConfig;
    
    public class JedisPoolUtils {
    
        private static JedisPool pool = null;
    
        static {
    
            // 加载配置文件
            InputStream in = JedisPoolUtils.class.getClassLoader().getResourceAsStream("redis.properties");
            Properties pro = new Properties();
            try {
                pro.load(in);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            // 获得池子对象
            JedisPoolConfig poolConfig = new JedisPoolConfig();
            poolConfig.setMaxIdle(Integer.parseInt(pro.get("redis.maxIdle").toString()));// 最大闲置个数
            poolConfig.setMinIdle(Integer.parseInt(pro.get("redis.minIdle").toString()));// 最小闲置个数
            poolConfig.setMaxTotal(Integer.parseInt(pro.get("redis.maxTotal").toString()));// 最大连接数
            pool = new JedisPool(poolConfig, pro.getProperty("redis.url"),
                    Integer.parseInt(pro.get("redis.port").toString()));
        }
    
        // 获得Jedis资源
        public static Jedis getJedis() {
            return pool.getResource();
        }
    }

    在src下新建一个文件:redis.properties:

    redis.maxIdle=30
    redis.minIdle=10
    redis.maxTotal=100
    redis.url=localhost
    redis.port=6379
  • 相关阅读:
    jQuery+ThinkPHP+Ajax实现即时消息提醒功能
    依赖注入(DI)
    控制反转(IoC)
    ajax以base64上传图片到django
    python 多进程、多线程、协程
    python3 实现tcp/udp局域网通信
    同源策略与跨域访问
    nginx+uwsgi阿里云ubuntu服务器上简单部署flask RESTful风格项目
    python3 实现 websocket server 解决中文乱码
    python IO 多路复用 epoll简单模型
  • 原文地址:https://www.cnblogs.com/xuyiqing/p/8850001.html
Copyright © 2020-2023  润新知