Redis-6. Redis集羣與Spring的整合


一、java操作redis集羣


Java操作集羣


Set<HostAndPort> jedisClusterNodes=new HashSet<HostAndPort>();

jedisClusterNodes.add(new HostAndPort("192.168.1.124",7001));

jedisClusterNodes.add(new HostAndPort("192.168.1.124",7002));

jedisClusterNodes.add(new HostAndPort("192.168.1.124",7003));

jedisClusterNodes.add(new HostAndPort("192.168.1.124",7004));

jedisClusterNodes.add(new HostAndPort("192.168.1.124",7005));

jedisClusterNodes.add(new HostAndPort("192.168.1.124",7006));



JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();

jedisPoolConfig.setMaxTotal(100);

jedisPoolConfig.setMaxIdle(20);

jedisPoolConfig.setMaxWaitMillis(-1);

jedisPoolConfig.setTestOnBorrow(true);


//節點,超時時間,最多重定向次數,鏈接池

JedisCluster jedisCluster=new JedisCluster(jedisClusterNodes,6000,1000,jedisPoolConfig);

System.out.println(jedisCluster.set("jedisClustername", "jedisClusterwlan"));

System.out.println(jedisCluster.set("jedisClustersex", "男"));

System.out.println(jedisCluster.get("jedisClustername"));

jedisCluster.close();



二、spring整合redis集羣

    

  程序啓動時,將數據緩存到redis中,如果修改操作再將緩存中的數據更新。針對一些不經常修改但是高訪問率的數據進行緩存來減少關係型數據庫的壓力



1、redis.xml配置文件中


//redis線程池的配置

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">

    <property name="maxTotal" value="10"/>

    <property name="maxIdle" value="8"/>

    <property name="minIdle" value="0"/>

    <property name="maxWaitMillis" value="5000"/>

    <property name="minEvictableIdleTimeMillis" value="300000"/>

    <property name="softMinEvictableIdleTimeMillis" value="-1"/>

    <property name="numTestsPerEvictionRun" value="3"/>

</bean>

//redis集羣的配置

<bean id="redisClusterConfig" class="org.springframework.data.redis.connection.RedisClusterConfiguration">

    <constructor-arg>

    <list>

    <value>192.168.90.124:7001</value>

    <value>192.168.90.124:7002</value>

    <value>192.168.90.124:7003</value>

    <value>192.168.90.124:7004</value>

    <value>192.168.90.124:7005</value>

    <value>192.168.90.124:7006</value>

    </list>

    </constructor-arg>

    <property name="maxRedirects" value="5"/>

</bean>


//redis集羣的連接工廠

<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">

     <constructor-arg index="0">

       <ref bean="redisClusterConfig"/>

     </constructor-arg>

     <constructor-arg index="1">

       <ref bean="jedisPoolConfig"/>

     </constructor-arg>

     <property name="database" value="0"/>

</bean>

或者單個redis的連接工廠配置

<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">

  <property name="poolConfig" ref="jedisPoolConfig" />

  <property name="hostName" value="192.168.90.124" />

  <property name="port" value="6379" />

  <property name="timeout" value="15000" />

  <property name="usePool" value="true" />

</bean>


//redis操作模板

<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">

    <property name="connectionFactory" ref="jedisConnectionFactory"/>

</bean>



RedisTemplate源碼中有以下幾個對象

//RedisTemplate中封裝了操作對象,有如下幾個:

private ValueOperations<K, V> valueOps;

private ListOperations<K, V> listOps;

private SetOperations<K, V> setOps;

private ZSetOperations<K, V> zSetOps;

private HyperLogLogOperations<K, V> hllOps;



2. Spring中使用RedisTemplate模板,封裝幾種類型的操作


//整合redis各種類型操作,在service中注入該對象,並調用響應的操作

@Service("redisService")

public class RedisService<T> {


    @Autowired //注入RedisTemplate

    public RedisTemplate redisTemplate;


    public <T> ValueOperations<String,T> setCacheObject(String key,T value){

      ValueOperations<String,T> operation=redisTemplate.opsForValue();

      operation.set(key, value);

      return operation;

    }

    public <T> T getCacheObject(String key){

      ValueOperations<String,T> operation=redisTemplate.opsForValue();

      return operation.get(key);

    }

}


4. spring redis緩存使用:註解


(1)在redis.xml配置文件中添加

//redis緩存管理器

<bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">

    <constructor-arg index="0">

     <ref bean="redisTemplate" />

    </constructor-arg>

     <!-- 默認有效期1h -->

    <property name="defaultExpiration" value="3600"/>

</bean>

<!-- 緩存註解開啓 -->

<cache:annotation-driven cache-manager="redisCacheManager"/>


(2)項目中操作

@CacheConfig(cacheNames="prizeinfo")

@Service(value="springRedisService")

public class SpringRedisServiceImpl implements SpringRedisService{

     @Autowired

     private PrizeinfoService prizeinfoService;

    

     @Cacheable(key="'id:'+#id")

     public Prizeinfo getObject(int id) {

       return prizeinfoService.getPrizeinfoById(id);

     }

     @CachePut(key="'id:'+#o.id")

     public int insertObject(Prizeinfo o) {

       return prizeinfoService.insertPrizeinfo(o);

     }

     @CachePut(key="'id:'+#prizeinfo.id")

     public int updateObject(Prizeinfo prizeinfo) {

       return prizeinfoService.updatePrizeinfo(prizeinfo);

     }

     @CacheEvict(key="'id:'+#id")

     public int deleteObjectById(int id) {

       return prizeinfoService.deletePrizeinfoById(id);

     }

}








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章