您现在的位置是:网站首页> 编程资料编程资料
springmvc集成使用redis过程_Redis_
2023-05-27
466人已围观
简介 springmvc集成使用redis过程_Redis_
Redis安装
首先安装redis。这个就不重点介绍了。windos下载redis就行。
我用的是mac
用命令行安装的。
安装命令
yum install redis
运行命令
sudo redis-server
这样就安装运行成功了。
spring集成redis
首先你需要下载驱动包,下载 jedis.jar,确保下载最新驱动包。然后导包。
在spring配置文件里我这是ApplicationContext .xml文件添加
然后用创建spring-redis.xml文件
写入
public class RedisCache { public final static String CAHCENAME = "niitcache";// 缓存名 public final static int CAHCETIME = 60;// 默认缓存时间 60S public final static int CAHCEHOUR = 60 * 60;// 默认缓存时间 1hr public final static int CAHCEDAY = 60 * 60 * 24;// 默认缓存时间 1Day public final static int CAHCEWEEK = 60 * 60 * 24 * 7;// 默认缓存时间 1week public final static int CAHCEMONTH = 60 * 60 * 24 * 7 * 30;// 默认缓存时间 1month @Autowired private RedisTemplateredisTemplate; public boolean putCache(String key, T obj) { final byte[] bkey = key.getBytes(); final byte[] bvalue = ProtoStuffSerializerUtil.serialize(obj); boolean result = redisTemplate.execute(new RedisCallback () { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { return connection.setNX(bkey, bvalue); } }); return result; } public void putCacheWithExpireTime(String key, T obj, final long expireTime) { final byte[] bkey = key.getBytes(); final byte[] bvalue = ProtoStuffSerializerUtil.serialize(obj); redisTemplate.execute(new RedisCallback () { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { connection.setEx(bkey, expireTime, bvalue); return true; } }); } public boolean putListCache(String key, List objList) { final byte[] bkey = key.getBytes(); final byte[] bvalue = ProtoStuffSerializerUtil.serializeList(objList); boolean result = redisTemplate.execute(new RedisCallback () { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { return connection.setNX(bkey, bvalue); } }); return result; } public boolean putListCacheWithExpireTime(String key, List objList, final long expireTime) { final byte[] bkey = key.getBytes(); final byte[] bvalue = ProtoStuffSerializerUtil.serializeList(objList); boolean result = redisTemplate.execute(new RedisCallback () { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { connection.setEx(bkey, expireTime, bvalue); return true; } }); return result; } public T getCache(final String key, Class targetClass) { byte[] result = redisTemplate.execute(new RedisCallback () { @Override public byte[] doInRedis(RedisConnection connection) throws DataAccessException { return connection.get(key.getBytes()); } }); if (result == null) { return null; } return ProtoStuffSerializerUtil.deserialize(result, targetClass); } public List getListCache(final String key, Class targetClass) { byte[] result = redisTemplate.execute(new RedisCallback () { @Override public byte[] doInRedis(RedisConnection connection) throws DataAccessException { return connection.get(key.getBytes()); } }); if (result == null) { return null; } return ProtoStuffSerializerUtil.deserializeList(result, targetClass); } /** * 精确删除key * * @param key */ public void deleteCache(String key) { redisTemplate.delete(key); } /** * 模糊删除key * * @param pattern */ public void deleteCacheWithPattern(String pattern) { Set keys = redisTemplate.keys(pattern); redisTemplate.delete(keys); } /** * 清空所有缓存 */ public void clearCache() { deleteCacheWithPattern(RedisCache.CAHCENAME + "|*"); } }
创建redis的配置文件 redis.properties。
写入
#redis config redis.pass= redis.pool.maxTotal=105 redis.pool.maxIdle=10 redis.pool.maxWaitMillis=5000 redis.pool.testOnBorrow=true redis.ip=127.0.0.1 redis.port=6379
这些根据自己的需求自定义配置就好了
这样redis就继承好了
SpringMVC中使用redis
创建一个redisCache类
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataAccessException; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import com.niit.util.ProtoStuffSerializerUtil; import java.util.List; import java.util.Set; /** * redis缓存 * * @author James * */ @Component public class RedisCache { public final static String CAHCENAME = "niitcache";// 缓存名 public final static int CAHCETIME = 60;// 默认缓存时间 60S public final static int CAHCEHOUR = 60 * 60;// 默认缓存时间 1hr public final static int CAHCEDAY = 60 * 60 * 24;// 默认缓存时间 1Day public final static int CAHCEWEEK = 60 * 60 * 24 * 7;// 默认缓存时间 1week public final static int CAHCEMONTH = 60 * 60 * 24 * 7 * 30;// 默认缓存时间 1month @Autowired private RedisTemplateredisTemplate; public boolean putCache(String key, T obj) { final byte[] bkey = key.getBytes(); final byte[] bvalue = ProtoStuffSerializerUtil.serialize(obj); boolean result = redisTemplate.execute(new RedisCallback () { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { return connection.setNX(bkey, bvalue); } }); return result; } public void putCacheWithExpireTime(String key, T obj, final long expireTime) { final byte[] bkey = key.getBytes(); final byte[] bvalue = ProtoStuffSerializerUtil.serialize(obj); redisTemplate.execute(new RedisCallback () { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { connection.setEx(bkey, expireTime, bvalue); return true; } }); } public boolean putListCache(String key, List objList) { final byte[] bkey = key.getBytes(); final byte[] bvalue = ProtoStuffSerializerUtil.serializeList(objList); boolean result = redisTemplate.execute(new RedisCallback () { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { return connection.setNX(bkey, bvalue); } }); return result; } public boolean putListCacheWithExpireTime(String key, List objList, final long expireTime) { final byte[] bkey = key.getBytes(); final byte[] bvalue = ProtoStuffSerializerUtil.serializeList(objList); boolean result = redisTemplate.execute(new RedisCallback () { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { connection.setEx(bkey, expireTime, bvalue); return true; } }); return result; } public T getCache(final String key, Class targetClass) { byte[] result = redisTemplate.execute(new RedisCallback () { @Override public byte[] doInRedis(RedisConnection connection) throws DataAccessException { return connection.get(key.getBytes()); } }); if (result == null) { return null; } return ProtoStuffSerializerUtil.deserialize(result, targetClass); } public List getListCache(final String key, Class targetClass) { byte[] result = redisTemplate.execute(new RedisCallback () { @Override public byte[] doInRedis(RedisConnection connection) throws DataAccessException { return connection.get(key.getBytes()); } }); if (result == null) { return null; } return ProtoStuffSerializerUtil.deserializeList(result, targetClass); } /** * 精确删除key * * @param key */ public void deleteCache(String key) { redisTemplate.delete(key); } /** * 模糊删除key * * @param pattern */ public void deleteCacheWithPattern(String pattern) { Set keys = redisTemplate.keys(pattern); redisTemplate.delete(keys); } /** * 清空所有缓存 */ public void clearCache() { deleteCacheWithPattern(RedisCache.CAHCENAME + "|*"); } }
写进和读取redis
String v = "test"; cache.putCacheWithExpireTime("key", v, cache.CAHCE
相关内容
- Redis实现分布式锁(setnx、getset、incr)以及如何处理超时情况_Redis_
- 大白话讲解调用Redis的increment失败原因及推荐使用详解_Redis_
- redis用list做消息队列的实现示例_Redis_
- Redis哨兵模式介绍_Redis_
- Redis实现排名功能的示例代码_Redis_
- Redis命令处理过程源码解析_Redis_
- Redis+Lua脚本实现计数器接口防刷功能(升级版)_Redis_
- Spring Boot实战解决高并发数据入库之 Redis 缓存+MySQL 批量入库问题_Redis_
- 基于Redis zSet实现滑动窗口对短信进行防刷限流的问题_Redis_
- Redis exists命令bug分析(案例详解)_Redis_
点击排行
本栏推荐
