JedisClient redis stand-alone operation and Cluster Edition

First, add a dependency in the pom file

<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>3.0.1</version>
</dependency>

Second, prepare JedisClient Interface

public interface JedisClient {

    String set(String key, String value);
    String get(String key);
    Boolean exists(String key);
    Long expire(String key, int seconds);
    Long ttl(String key);
    Long incr(String key);
    Long hset(String key, String field, String value);
    String hget(String key, String field);
    Long hdel(String key, String... field);
}

Third, to achieve

1. Single Version

public class JedisClientPool implements JedisClient {
	
	@Autowired
	private JedisPool jedisPool;

	@Override
	public String set(String key, String value) {
		Jedis jedis = jedisPool.getResource();
		String result = jedis.set(key, value);
		jedis.close();
		return result;
	}

	@Override
	public String get(String key) {
		Jedis jedis = jedisPool.getResource();
		String result = jedis.get(key);
		jedis.close();
		return result;
	}

	@Override
	public Boolean exists(String key) {
		Jedis jedis = jedisPool.getResource();
		Boolean result = jedis.exists(key);
		jedis.close();
		return result;
	}

	@Override
	public Long expire(String key, int seconds) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.expire(key, seconds);
		jedis.close();
		return result;
	}

	@Override
	public Long ttl(String key) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.ttl(key);
		jedis.close();
		return result;
	}

	@Override
	public Long incr(String key) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.incr(key);
		jedis.close();
		return result;
	}

	@Override
	public Long hset(String key, String field, String value) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.hset(key, field, value);
		jedis.close();
		return result;
	}

	@Override
	public String hget(String key, String field) {
		Jedis jedis = jedisPool.getResource();
		String result = jedis.hget(key, field);
		jedis.close();
		return result;
	}

	@Override
	public Long hdel(String key, String... field) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.hdel(key, field);
		jedis.close();
		return result;
	}

}

  (2) Cluster Edition

public class JedisClientCluster implements JedisClient {
	
	@Autowired
	private JedisCluster jedisCluster;

	@Override
	public String set(String key, String value) {
		return jedisCluster.set(key, value);
	}

	@Override
	public String get(String key) {
		return jedisCluster.get(key);
	}

	@Override
	public Boolean exists(String key) {
		return jedisCluster.exists(key);
	}

	@Override
	public Long expire(String key, int seconds) {
		return jedisCluster.expire(key, seconds);
	}

	@Override
	public Long ttl(String key) {
		return jedisCluster.ttl(key);
	}

	@Override
	public Long incr(String key) {
		return jedisCluster.incr(key);
	}

	@Override
	public Long hset(String key, String field, String value) {
		return jedisCluster.hset(key, field, value);
	}

	@Override
	public String hget(String key, String field) {
		return jedisCluster.hget(key, field);
	}

	@Override
	public Long hdel(String key, String... field) {
		return jedisCluster.hdel(key, field);
	}

  Fourth, the configuration file

<!-- redis单机版 -->
	<!-- <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
		<constructor-arg name="host" value="192.168.206.132"/>	
		<constructor-arg name="port" value="7001"/>	
	</bean>
	<bean id="jedisClientPool" class="com.taotao.jedis.JedisClientPool"/> -->
	<!-- redis集群 -->
	<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
		<constructor-arg>
			<set>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.206.132"/>
					<constructor-arg name="port" value="7001"/>
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.25.132"/>
					<constructor-arg name="port" value="7002"/>
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.25.132"/>
					<constructor-arg name="port" value="7003"/>
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.206.132"/>
					<constructor-arg name="port" value="7004"/>
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.206.132"/>
					<constructor-arg name="port" value="7005"/>
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.206.132"/>
					<constructor-arg name="port" value="7006"/>
				</bean>
			</set>
		</constructor-arg>
	</bean>
	<bean id="jedisClientCluster" class="com.taotao.jedis.JedisClientCluster"/>

  

Five test

   @Test 
    public void testJedis () { 
        // Initialization spring containers 
        the ApplicationContext applicationContext the ClassPathXmlApplicationContext new new = ( "CLASSPATH: spring / applicationContext-redis.xml"); 
        // get the object from the container jedisclient 
        JedisClient jedisClient = applicationContext.getBean (JedisClient.class ); 
        // use JedisClient operation Redis 
        jedisClient.set ( "OK", "test666"); 
        String jedisClient.get Result = ( "OK"); 
        System.out.println (Result); 
    }

 Six, adding business logic to cache

Note: Add the cache does not affect the normal business logic

  At query time, businesses should first go to the query cache

  If the cache is not in the database query

  Adding to the database query results into cache

  The recommended data type hash

The following code:

public List <TbContent> getContentList ( Long CID) {
         // query cache 
        the try { 
            String json = jedisClient.hget (CONTENT_KEY, CID + "" );
             // determines whether the air json 
            IF (StringUtils.isNotBlank (json)) {
                 / / converted into json list 
                list <TbContent> list = JsonUtils.jsonToList (json, TbContent. class );
                 return list; 
            } 
        } the catch (Exception E) { 
            e.printStackTrace (); 
        } 
        // The query list cid
        = Example TbContentExample new new TbContentExample ();
         // Set the search criteria 
        Criteria Criteria = example.createCriteria (); 
        criteria.andCategoryIdEqualTo (CID); 
        // execute the query 
        List <TbContent> List = contentMapper.selectByExample (Example);
         // the buffer data added 
        the try { 
            jedisClient.hset (CONTENT_KEY, CID + "" , JsonUtils.objectToJson (List)); 
        } the catch (Exception E) { 
            e.printStackTrace (); 
        } 
        return List; 
    }

Seven, cache synchronization

  After the contents of the information to make additions and deletions to the operation just need to delete the corresponding cache

        // cache synchronization 
        jedisClient.hdel (CONTENT_KEY, content.getCategoryId () toString ().);

 

Guess you like

Origin www.cnblogs.com/wanerhu/p/11241631.html