redis.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- <context:property-placeholder location="classpath:*.properties"/> -->
<context:property-placeholder location="classpath:redis.properties"/>
<!--设置数据池-->
<bean ></property>
</bean>
<bean />
</property>-->
</bean>
</beans>
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
redis.properties配置文件
#redis setting
redis.maxIdle=300
redis.minIdle=100
redis.maxWaitMillis=3000
redis.testOnBorrow=true
redis.maxTotal=500
redis.host=127.0.0.1
redis.port=6379
redis.password=
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
redis操作抽象类 jedis
package cn.mybatis.Util.redis;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import javax.annotation.Resource;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* Created by cjq on 2017/12/05.
*/
public abstract class IRedisService<T> {
@Autowired
protected RedisTemplate<String, Object> redisTemplate;
/* @Resource
protected HashOperations<String, String, T> hashOperations;*/
/**
* 存入redis中的key
*
* @return
*/
protected abstract String getRedisKey();
/**
* 添加
*
* @param key key
* @param doamin 对象
* @param expire 过期时间(单位:秒),传入 -1 时表示不设置过期时间
*/
public void put(String key, T doamin, long expire) {
redisTemplate.opsForHash().put(getRedisKey(), key, doamin);
if (expire != -1) {
redisTemplate.expire(getRedisKey(), expire, TimeUnit.SECONDS);
}
}
/**
* 删除
*
* @param key 传入key的名称
*/
public void remove(String key) {
redisTemplate.opsForHash().delete(getRedisKey(), key);
}
/**
* 查询
*
* @param key 查询的key
* @return
*/
public Object get(String key) {
return redisTemplate.opsForHash().get(getRedisKey(), key);
}
/**
* 获取当前redis库下所有对象
*
* @return
*/
public List<Object> getAll() {
return redisTemplate.opsForHash().values(getRedisKey());
}
/**
* 查询查询当前redis库下所有key
*
* @return
*/
public Set<Object> getKeys() {
return redisTemplate.opsForHash().keys(getRedisKey());
}
/**
* 判断key是否存在redis中
*
* @param key 传入key的名称
* @return
*/
public boolean isKeyExists(String key) {
return redisTemplate.opsForHash().hasKey(getRedisKey(), key);
}
/**
* 查询当前key下缓存数量
*
* @return
*/
public long count() {
return redisTemplate.opsForHash().size(getRedisKey());
}
/**
* 清空redis
*/
public void empty() {
Set<Object> set = redisTemplate.opsForHash().keys(getRedisKey());
for(Object key : set){
redisTemplate.opsForHash().delete(getRedisKey(), key);
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
redis抽象类的子类作为service使用 rediskey识别一个redis库
package cn.mybatis.Util.redis;
import org.springframework.stereotype.Service;
import cn.mybatis.Model.User;
/**
* Created by cjq on 2017/12/05.
*
*/
@Service
public class UserRedisServiceImpl extends IRedisService<User>{
private String redisKey = "UserRedisData";
public String getRedisKey() {
return this.redisKey;
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
实例中的使用
注入抽象类子类
@Autowired
private UserRedisServiceImpl userRedisServiceImpl;
存入对象数据
userRedisServiceImpl.put(user.getAccount(), user, -1);
清空所有数据
userRedisServiceImpl.empty();
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
question1:
redis配置文件应尽早加载 不然会读取不到properties配置的值;quarzt配置文件应尽早加载,不然注解的定时任务无法启动
question2:
clean 项目重新部署一次启动成功
question3:普通类无法使用注入,否则为null,只有spring容器下的类才可以使用注入形式
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:SSM整合redis - Python技术站