redis的安装直接跳过
1.注册redis服务
在DOM窗口下,进入redis的安装目录(可以先进入安装目录,然后shift+右键,选择在此处打开powershell窗口),
输入命令:
redis-server --service-install redis.windows-service.conf --loglevel verbose;
开启redis服务
打开DOM窗口,输入命令:
redis-server --service-start
关闭redis服务
打开DOM窗口,输入命令:
redis-server --service-stop
到此,redis在windows下的配置便告完成。
2.jedis api的使用
maven引入jedis的依赖:
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.3.2</version> </dependency>
redis.properties文件定义了jedis连接池的一些参数和redis服务器的信息
#config for redis #最大连接数 redis.pool.maxActive=512 #最大空闲连接数 redis.pool.maxIdle=100 #最大等待等待时间 redis.pool.maxWait=100000 redis.pool.testOnBorrow=true redis.pool.testOnReturn=true redis.ip=127.0.0.1 redis.port=6379 #设置过期时间为2小时,超过过期时间会自动删除key-value redis.expire=7200注意:在实际的项目文件中不允许出现中文注解
RedisProvider类定义如何获得与redis的连接和释放与redis的连接
public class RedisProvider { //protected static final Logger LOG = LoggerFactory.getLogger(RedisProvider.class); protected static JedisPool jedispool; protected static int EXPIRE = 130; static{ ResourceBundle bundle = ResourceBundle.getBundle("redis"); if (bundle == null) { throw new IllegalArgumentException( "[redis.properties] is not found!"); } EXPIRE = Integer.valueOf(bundle.getString("redis.expire")); JedisPoolConfig jedisconfig = new JedisPoolConfig(); jedisconfig.setMaxActive(Integer.valueOf(bundle .getString("redis.pool.maxActive"))); jedisconfig.setMaxIdle(Integer.valueOf(bundle .getString("redis.pool.maxIdle"))); jedisconfig.setMaxWait(Long.valueOf(bundle .getString("redis.pool.maxWait"))); jedisconfig.setTestOnBorrow(Boolean.valueOf(bundle .getString("redis.pool.testOnBorrow"))); jedisconfig.setTestOnReturn(Boolean.valueOf(bundle .getString("redis.pool.testOnReturn"))); jedispool = new JedisPool(jedisconfig, bundle.getString("redis.ip"), Integer.valueOf(bundle.getString("redis.port")), 100000); } /*获取资源*/ public static Jedis getJedis() { Jedis jedis = null; try { jedis = jedispool.getResource(); } catch (JedisConnectionException jce) { ExceptionUtil.getTrace(jce); try { Thread.sleep(3000); } catch (InterruptedException e) { ExceptionUtil.getTrace(e); } jedis = jedispool.getResource(); } return jedis; } /*释放资源*/ public static void returnResource(JedisPool pool, Jedis jedis) { if (jedis != null) { pool.returnResource(jedis); } } } /*资源jedis理解为连接。jedispool理解为连接池*/
RedisHelper定义了利用jedis连接堆redis服务器端进行的各种操作,这里出于应用的需要,只实现了基本的set和get操作,更多的jedis操作可见它的api:
https://tool.oschina.net/uploads/apidocs/
public class RedisHelper extends RedisProvider { /** * Set the string value as value of the key. Default settings at save * time(2000s) * * @param key * @param value * @return */ public static String set(String key, String value) { Jedis jedis = null; String rtn = null; try { jedis = getJedis(); rtn = jedis.setex(key, EXPIRE, value); } catch (Exception e) { //LOG.error(ExceptionUtil.getTrace(e)); jedispool.returnBrokenResource(jedis); } finally { returnResource(jedispool, jedis); } return rtn; } public static String get(String key) { Jedis jedis = null; String rtn = null; try { jedis = getJedis(); rtn = jedis.get(key); } catch (Exception e) { //LOG.error(ExceptionUtil.getTrace(e)); jedispool.returnBrokenResource(jedis); } finally { returnResource(jedispool, jedis); } return rtn; } }
来源:https://www.cnblogs.com/chxyshaodiao/p/12292263.html