欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

基于spring-data-redis的redis缓存的Serive实现(代码记录)

程序员文章站 2022-05-26 14:02:24
...

这个只是一次代码记录,是redies的Service层实现

public class RedisCacheServiceImpl implements RedisCacheService {
	@Autowired
	public RedisTemplate<String, Object> redisTemplate;
	
	public ValueOperations<String, Object> opsForValue(){
		return redisTemplate.opsForValue();
	}
	
	public HashOperations<String, Object, Object> opsForHash(){
		return redisTemplate.opsForHash();
	}
	
	public  ListOperations<String, Object> opsForList(){
		return redisTemplate.opsForList();
	}
	
	public SetOperations<String, Object>  opsForSet(){
		return redisTemplate.opsForSet();
	}
	
	
	public boolean  isExist(String key){
		return redisTemplate.hasKey(key);
	}
	
	public void delete(String key){
		redisTemplate.delete(key);
	}
	
	public void delete(Collection<String> keys){
		redisTemplate.delete(keys);
	}


	public void clear(String pattern) {
		Set<String> keys = redisTemplate.keys(pattern);
		if (!keys.isEmpty()) {
			redisTemplate.delete(keys);
		}
	}

}

spring配置redis

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="locations">  
            <list>  
                <value>classpath*:redis.properties</value>  
            </list>  
        </property>  
        <property name="ignoreUnresolvablePlaceholders" value="true" />   
    </bean>  
  
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">  
        <property name="maxTotal" value="${redis.maxTotal}"></property>  
        <property name="maxIdle" value="${redis.maxIdle}"></property>  
        <property name="minIdle" value="${redis.minIdle}"></property>  
        <property name="maxWaitMillis" value="${redis.maxWaitMillis}"></property>  
        <property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}"></property>  
        <property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}"></property>  
        <property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}"></property>  
        <property name="testOnBorrow" value="${redis.testOnBorrow}"></property>  
        <property name="testOnReturn" value="${redis.testOnReturn}"></property>  
        <property name="testWhileIdle" value="${redis.testWhileIdle}"></property>  
    </bean>  
  
    <bean id="jedisConnectionFactory"  
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"  
        destroy-method="destroy">  
        <property name="hostName" value="${redis.host}" />  
        <property name="port" value="${redis.port}" />  
        <property name="timeout" value="${redis.timeout}" />  
        <property name="database" value="${redis.database}" />  
        <property name="password" value="${redis.password}" />  
        <property name="usePool" value="${redis.usePool}" />  
        <property name="poolConfig" ref="jedisPoolConfig" />  
    </bean>  
  
    <!-- redis template definition p表示对该bean里面的属性进行注入,格式为p:属性名=注入的对象 效果与在bean里面使用<property>标签一样 -->  
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"  
        p:connection-factory-ref="jedisConnectionFactory">  
        <!-- 序列化方式 建议key/hashKey采用StringRedisSerializer。 -->  
        <property name="keySerializer">  
            <bean  
                class="org.springframework.data.redis.serializer.StringRedisSerializer" />  
        </property>  
        <property name="hashKeySerializer">  
            <bean  
                class="org.springframework.data.redis.serializer.StringRedisSerializer" />  
        </property>  
        <property name="valueSerializer">  
            <bean  
                class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />  
        </property>  
        <property name="hashValueSerializer">  
            <bean  
                class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />  
        </property>  
  
    </bean>  
    <!-- 对string操作的封装 -->  
    <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"  
        p:connection-factory-ref="jedisConnectionFactory" />  
<!--         <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager" c:template-ref="redisTemplate"/> -->  
          
          
     <!-- redis缓存服务 -->
	<bean id="redisCacheService"
		class="cas.ecafe.iot.utils.sevice.RedisCacheServiceImpl"></bean>

 

相关标签: 代码记录