Java Redis 连接池 Jedis 工具类,java基础面试笔试题


我总结出了很多互联网公司的面试题及答案,并整理成了文档,以及各种学习的进阶学习资料,免费分享给大家。
扫描二维码或搜索下图红色VX号,加VX好友,拉你进【程序员面试学习交流群】免费领取。也欢迎各位一起在群里探讨技术。
推荐文章:Java 面试知识点解析Mysql优化技巧(数据库设计、命名规范、索引优化


 

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class MyJedisPool { private final static Logger logger = LoggerFactory.getLogger(MyJedisPool.class); private static JedisPool readPool = null; private static JedisPool writePool = null; //静态代码初始化池配置 static { try{ Properties props = new Properties(); InputStream in = MyJedisPool.class.getResourceAsStream("/redis.properties"); props.load(in); //创建jedis池配置实例 JedisPoolConfig config = new JedisPoolConfig(); //设置池配置项值 config.setMaxTotal(Integer.valueOf(props.getProperty("jedis.pool.maxActive"))); config.setMaxIdle(Integer.valueOf(props.getProperty("jedis.pool.maxIdle"))); config.setMaxWaitMillis(Long.valueOf(props.getProperty("jedis.pool.maxWait"))); config.setTestOnBorrow(Boolean.valueOf(props.getProperty("jedis.pool.testOnBorrow"))); config.setTestOnReturn(Boolean.valueOf(props.getProperty("jedis.pool.testOnReturn"))); //根据配置实例化jedis池 readPool = new JedisPool(config, props.getProperty("redisReadURL"), Integer.valueOf(props.getProperty("redisReadPort"))); writePool = new JedisPool(config, props.getProperty("redisWriteURL"), Integer.valueOf(props.getProperty("redisWritePort"))); }catch (IOException e) { logger.info("redis连接池异常",e); } } /**获得jedis对象*/ public static Jedis getReadJedisObject(){ return readPool.getResource(); } /**获得jedis对象*/ public static Jedis getWriteJedisObject(){ return writePool.getResource(); } /**归还jedis对象*/ public static void returnJedisOjbect(Jedis jedis){ if (jedis != null) { jedis.close(); } } }

import redis.clients.jedis.Jedis;

import java.util.Set;

public class RedisUtils {

    /**

     * 获取hash表中所有key

     * @param name

     * @return

     */

    public static Set<String> getHashAllKey(String name){

        Jedis jedis = null;

        try {

            jedis = MyJedisPool.getReadJedisObject();

            return jedis.hkeys(name);

        }catch (Exception e){

            e.printStackTrace();

        }finally {

            MyJedisPool.returnJedisOjbect(jedis);

        }

        return null;

    }

    /**

     * 从redis hash表中获取

     * @param hashName

     * @param key

     * @return

     */

    public static String getHashKV(String hashName,String key){

        Jedis jedis = null;

        try {

            jedis = MyJedisPool.getReadJedisObject();

            return jedis.hget(hashName, key);

        }catch (Exception e){

            e.printStackTrace();

        }finally {

            MyJedisPool.returnJedisOjbect(jedis);

        }

        return null;

    }

    /**

     * 删除hash表的键值对

     * @param hashName

     * @param key

     */

    public static Long delHashKV(String hashName,String key){

        Jedis jedis = null;

        try {

            jedis = MyJedisPool.getWriteJedisObject();

            return jedis.hdel(hashName,key);

        }catch (Exception e){

            e.printStackTrace();

        }finally {

            MyJedisPool.returnJedisOjbect(jedis);

        }

        return null;

    }

    /**

     * 存放hash表键值对

     * @param hashName

     * @param key

     * @param value

     */

    public static Long setHashKV(String hashName,String key,String value){

        Jedis jedis = null;

        try {

            jedis = MyJedisPool.getWriteJedisObject();

            return jedis.hset(hashName,key,value);

        }catch (Exception e){

            e.printStackTrace();

        }finally {

            MyJedisPool.returnJedisOjbect(jedis);

        }

        return null;

    }

    /**

     * 删除键值对

     * @param k

     * @return

     */

    public static Long delKV(String k){

        Jedis jedis = null;

        try {

            jedis = MyJedisPool.getWriteJedisObject();

            return jedis.del(k);

        }catch (Exception e){

            e.printStackTrace();

        }finally {

            MyJedisPool.returnJedisOjbect(jedis);

        }

        return null;

    }

    /**

     * 放键值对

     * 永久

     * @param k

     * @param v

     */

    public static String setKV(String k, String v)

    {

        Jedis jedis = null;

        try {

            jedis = MyJedisPool.getWriteJedisObject();

            return jedis.set(k, v);

        }catch (Exception e){

            e.printStackTrace();

        }finally {

            MyJedisPool.returnJedisOjbect(jedis);

        }

        return null;

    }

    /**

     * 放键值对

     *

     * @param k

     * @param v

     */

    public static String setKV(String k,int second, String v)

    {

        Jedis jedis = null;

        try {

            jedis = MyJedisPool.getWriteJedisObject();

            return jedis.setex(k,second, v);

        }catch (Exception e){

            e.printStackTrace();

        }finally {

            MyJedisPool.returnJedisOjbect(jedis);

        }

        return null;

    }

    /**

     * 根据key取value

     *

     * @param k

     * @return

     */

    public static String getKV(String k)

    {

        Jedis jedis = null;

        try {

            jedis = MyJedisPool.getReadJedisObject();

            return jedis.get(k);

        }catch (Exception e){

            e.printStackTrace();

        }finally {

            MyJedisPool.returnJedisOjbect(jedis);

        }

        return null;

    }

}



import java.util.Set;

public class RedisUtils {

    /**

     * 获取hash表中所有key

     * @param name

     * @return

     */

    public static Set<String> getHashAllKey(String name){

        Jedis jedis = null;

        try {

            jedis = MyJedisPool.getReadJedisObject();

            return jedis.hkeys(name);

        }catch (Exception e){

            e.printStackTrace();

        }finally {

            MyJedisPool.returnJedisOjbect(jedis);

        }

        return null;

    }

    /**

     * 从redis hash表中获取

     * @param hashName

     * @param key

     * @return

     */

    public static String getHashKV(String hashName,String key){

        Jedis jedis = null;

        try {

            jedis = MyJedisPool.getReadJedisObject();

            return jedis.hget(hashName, key);

        }catch (Exception e){

            e.printStackTrace();

        }finally {

            MyJedisPool.returnJedisOjbect(jedis);

        }

        return null;

    }

    /**

     * 删除hash表的键值对

     * @param hashName

     * @param key

     */

    public static Long delHashKV(String hashName,String key){

        Jedis jedis = null;

        try {

            jedis = MyJedisPool.getWriteJedisObject();

            return jedis.hdel(hashName,key);

        }catch (Exception e){

            e.printStackTrace();

        }finally {

            MyJedisPool.returnJedisOjbect(jedis);

        }

        return null;

    }

    /**

     * 存放hash表键值对

     * @param hashName

     * @param key

     * @param value

     */

    public static Long setHashKV(String hashName,String key,String value){

        Jedis jedis = null;

        try {

            jedis = MyJedisPool.getWriteJedisObject();

            return jedis.hset(hashName,key,value);

        }catch (Exception e){

            e.printStackTrace();

        }finally {

            MyJedisPool.returnJedisOjbect(jedis);

        }

        return null;

    }

    /**

     * 删除键值对

     * @param k

     * @return

     */

    public static Long delKV(String k){

        Jedis jedis = null;

        try {

            jedis = MyJedisPool.getWriteJedisObject();

            return jedis.del(k);

        }catch (Exception e){

            e.printStackTrace();

        }finally {

            MyJedisPool.returnJedisOjbect(jedis);

        }

        return null;

    }

    /**

     * 放键值对

     * 永久

     * @param k

     * @param v

     */

    public static String setKV(String k, String v)

    {

        Jedis jedis = null;

        try {

            jedis = MyJedisPool.getWriteJedisObject();

            return jedis.set(k, v);

        }catch (Exception e){

            e.printStackTrace();

        }finally {

            MyJedisPool.returnJedisOjbect(jedis);

        }

        return null;

    }

    /**

     * 放键值对

     *

     * @param k

     * @param v

     */

    public static String setKV(String k,int second, String v)

    {

        Jedis jedis = null;

        try {

            jedis = MyJedisPool.getWriteJedisObject();

            return jedis.setex(k,second, v);

        }catch (Exception e){

            e.printStackTrace();

        }finally {

            MyJedisPool.returnJedisOjbect(jedis);

        }

        return null;

    }

    /**

     * 根据key取value

     *

     * @param k

     * @return

     */

    public static String getKV(String k)

    {

        Jedis jedis = null;

        try {

            jedis = MyJedisPool.getReadJedisObject();

            return jedis.get(k);

        }catch (Exception e){

            e.printStackTrace();

        }finally {

            MyJedisPool.returnJedisOjbect(jedis);

        }

        return null;

    }

}


转载:https://www.cnblogs.com/yanqin/p/8342836.html

推荐内容:
JAVA给图片添加水印
【原创】架构师必备,带你弄清混乱的JAVA日志体系!
JAVA日志的前世今生
Java数据结构和算法(十四)——堆
Java实现单链表的快速排序和归并排序
java面试常考题
聊聊Java 8 Lambda 表达式
Java序列化机制原理
Java入门篇(五)——字符串/String类
【JVM系列】一步步解析java执行内幕

猜你喜欢

转载自blog.csdn.net/agoodcoder777/article/details/89684747