Java 操作Redis
一、 使用 Java 操作 Redis 前,请先运行 Redis 服务与下载 Redis 驱动,以 maven 工程为例,引入如下 jar
1 2 3 4 5 | < dependency > < groupId >redis.clients</ groupId > < artifactId >jedis</ artifactId > < version >2.9.0</ version > </ dependency > |
创建测试类,验证是否成功连接
1 2 3 4 5 6 7 8 9 10 11 12 | public static void main(String[] args) { //设置IP与端口,连接 Redis 服务 Jedis jedis = new Jedis( "127.0.0.1" , 6379 ); //设置 密码 jedis.auth( "1D9JJ9JIJ0003E4710AC0000596F1A72" ); //查看服务是否运行,打出pong表示OK System.out.println( "connection is OK==========>: " +jedis.ping()); String name = jedis.get( "admin" ); System.out.println( "姓名:" + name); String noticeVal = jedis.hget( "notice" , "admin" ); System.out.println( "通知结果:" + noticeVal); } |
执行成功,结果如下
1 2 3 | connection is OK==========>: PONG 姓名:null 通知结果:0 |
二、连接池管理 Redis 连接,引入 redis.properties 配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | # Redis settings # 最大闲置数 redis.maxIdle=30 # 最小闲置数 redis.minIdle=10 # 最大连接数 redis.maxTotal=100 # redis服务器的IP地址 redis.url=127.0.0.1 # redis端口 redis.port=6379 # redis密码 redis.password=1D9JJ9JIJ0003E4710AC0000596F1A72 # 超时时间 redis.timeout=2000 |
编写工具类 RedisUtil
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | package com.util; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; /** * RedisUtil工具类 * * @author Administrator * */ public final class RedisUtil { private static final Log logger = LogFactory.getLog(RedisUtil. class ); private static JedisPool jedisPool = null ; /** * 初始化Redis连接池 */ static { try { // 加载配置文件 InputStream in = RedisUtil. 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())); // 最大连接数 jedisPool = new JedisPool(poolConfig, pro.getProperty( "redis.url" ), Integer.parseInt(pro.getProperty( "redis.port" )), Integer.parseInt(pro.getProperty( "redis.timeout" )), pro.getProperty( "redis.password" )); } catch (Exception e) { logger.error( "异常错误" , e); } } /** * 获取Jedis实例 * * @return */ public synchronized static Jedis getJedis() { try { if (jedisPool != null ) { Jedis resource = jedisPool.getResource(); return resource; } else { return null ; } } catch (Exception e) { e.printStackTrace(); return null ; } } /** * 释放jedis资源 * * @param jedis */ public static void returnResource( final Jedis jedis) { if (jedis != null ) { jedis.close(); } } } |
测试
1 2 3 4 5 6 7 | public static void main(String[] args) { Jedis jedis = RedisUtil.getJedis(); String result = jedis.set( "admin" , "管理员" ); System.out.println( "结果:" + result); //释放资源 RedisUtil.returnResource(jedis); } |
1 | 结果:OK |
使用 RedisDesktopManager 可视化工具查看
其他数据类型如 hash(哈希)、list(列表)、set(集合)及 zset(有序集合) 的增删改查操作和 redis 原生指令大同小异,抽取为通用工具类。