万事第一步(导包):

    1. <!-- Jedis 操作Redis的框架-->
    2. <dependency>
    3. <groupId>redis.clients</groupId>
    4. <artifactId>jedis</artifactId>
    5. </dependency>

    第二步封装工具类:
    image.png
    image.png

    1. package com.mhy.boot_plus.utils;
    2. import redis.clients.jedis.GeoRadiusResponse;
    3. import redis.clients.jedis.GeoUnit;
    4. import redis.clients.jedis.Jedis;
    5. import java.util.List;
    6. import java.util.Map;
    7. /**
    8. * @author Feri
    9. * @date 2021/12/30 14:01
    10. * @description:TODO Java操作Redis
    11. */
    12. public class JedisUtils {
    13. private static Jedis jedis;
    14. static {
    15. //完成实例化,连接Redis服务器
    16. jedis=new Jedis("120.27.133.194",6380);
    17. jedis.auth("123");
    18. }
    19. //新增
    20. /**
    21. * String类型 新增*/
    22. public static void addString(String key,String value,long seconds){
    23. jedis.setex(key, seconds, value);
    24. }
    25. public static void addString(String key,String value){
    26. jedis.set(key, value);
    27. }
    28. public static void addHash(String key,String field,String value){
    29. jedis.hset(key, field, value);
    30. }
    31. public static void addHash(String key, Map<String,String> map){
    32. jedis.hmset(key, map);
    33. }
    34. public static void addList(String key,String value){
    35. jedis.lpush(key,value);
    36. }
    37. public static void addGeo(String key,double lon,double lat,String address){
    38. jedis.geoadd(key, lon, lat, address);
    39. }
    40. //校验
    41. public static boolean exists(String... keys){
    42. return jedis.exists(keys)>0;
    43. }
    44. public static boolean existsHash(String key,String field){
    45. return jedis.hexists(key, field);
    46. }
    47. //删除
    48. public static void delKey(String key){
    49. jedis.del(key);
    50. }
    51. public static void delHash(String key,String field){
    52. jedis.hdel(key,field);
    53. }
    54. //geo操作
    55. public static double geodist(String key,String address1,String address2){
    56. return jedis.geodist(key, address1, address2, GeoUnit.M);
    57. }
    58. public static List<GeoRadiusResponse> georadius(String key, double lon, double lat, double radius){
    59. return jedis.georadius(key,lon,lat,radius,GeoUnit.M);
    60. }
    61. //系统命令
    62. public static void expire(String key,long seconds){
    63. jedis.expire(key, seconds);
    64. }
    65. public static long ttl(String key){
    66. return jedis.ttl(key);
    67. }
    68. public static void main(String[] args) {
    69. //String类型
    70. jedis.set("str2112","Hello Word");//新增或修改
    71. System.err.println(jedis.get("str2112"));//获取
    72. System.err.println(jedis.strlen("str2112"));//长度
    73. //List类型
    74. jedis.lpush("list2112","1","2","3");
    75. jedis.rpush("list2112","a","b","c");
    76. //321abc
    77. System.err.println(jedis.lrange("list2112",0,jedis.llen("list2112")-1));
    78. System.err.println(jedis.lpop("list2112"));
    79. System.err.println(jedis.rpop("list2112"));
    80. //Hash类型
    81. jedis.hset("hash2112","1","www");
    82. System.err.println(jedis.hget("hash2112","1"));
    83. System.err.println(jedis.hexists("hash2112","1"));
    84. //Set类型
    85. jedis.sadd("set2112","w","e","r");
    86. System.err.println(jedis.smembers("set2112"));
    87. System.err.println(jedis.sismember("set2112","w"));
    88. }
    89. }