JAva webプロジェクトにおけるredisクラスタまたはクリック版構成の詳細

10739 ワード

スタンドアロン構成
 
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
     
     
        
        
        

        
     
    

クラスタ版構成、constructor-arg name=“host”value=“192.168.199.23”指定ip交換でOK

        
            
                
                    
                    
                
                
                    
                    
                
                
                    
                    
                
                
                    
                    
                
                
                    
                    
                
                
                    
                    
                
            
        
        
    
     

2つのクライアントコードクライアントインタフェース
/**
 * Copyright (c) 2017. yagoosafe.com All right reserved. This software is the
 * confidential and proprietary information of yagoosafe.com ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use it only in accordance
 * with the terms of the license agreement you entered into with yagoosafe.com.
 */
package com.yagoo.wificontrolsys.redis;

/** 
 *   :JedisClient.java
 *   :jedis client
 *   :2018 3 8    4:59:30 
 * @author yangchangjiang  
 * @version 1.0 
 */
public interface JedisClient {
    /**
     * 
     *   key    
     * @param key
     * @return String
     */
    String get(String key);
    /**
     * 
     *     
     * @param key
     * @param value
     * @return String
     */
    String set(String key,String value);
    /**
     * 
     *          
     * @param key
     * @param value
     * @param expire
     * @return String
     */
    String set(String key, String value, int expire);
    /**
     * 
     * hset   key 
     * @param hkey
     * @param key
     * @return String
     */
    String hget(String hkey,String key);
    /**
     * 
     * hset   key  value 
     * @param hkey
     * @param key
     * @param value
     * @return long
     */
    long hset(String hkey,String key,String value);
    /**
     * 
     * Incr  +1
     * @param key
     * @return long
     */
    long incr(String key);
    /**
     * 
     *       
     * @param key
     * @param second
     * @return long
     */
    long expire(String key,int second);
    /**
     * 
     *       
     * @param key
     * @return long
     */
    long ttl(String key);
    /**
     * 
     *     key 
     * @param key
     * @return long
     */
    long del(String key);
    /**
     * 
     *   hkey key
     * @param hkey
     * @param key
     * @return long
     */
    long hdel(String hkey,String key);
}

スタンドアロン版実装
/*
 * Copyright 2017 wenwuyi.cn All right reserved. This software is the
 * confidential and proprietary information of yagoosafe.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with wenwuyi.cn.
 */
package com.yagoo.wificontrolsys.redis.impl;

import org.springframework.beans.factory.annotation.Autowired;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import com.yagoo.wificontrolsys.redis.JedisClient;

/** 
 *     JedisClientSingle.java 
 *     :Redis   
 * @author YCJ     E-mail: [email protected]
 * @date     :2017 12 24    12:51:39 
 * @version 1.0 
 */
public class JedisClientSingle implements JedisClient{

    @Autowired
    private JedisPool jedisPool; 

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

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

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

    @Override
    public long hset(String hkey, String key, String value) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.hset(hkey, key, value);
        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 expire(String key, int second) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.expire(key, second);
        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 del(String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.del(key);
        jedis.close();
        return result;
    }

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

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

}

クラスタ版実装
/*
 * Copyright 2017 wenwuyi.cn All right reserved. This software is the
 * confidential and proprietary information of yagoosafe.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with wenwuyi.cn.
 */
package com.yagoo.wificontrolsys.redis.impl;

import org.springframework.beans.factory.annotation.Autowired;

import redis.clients.jedis.JedisCluster;

import com.yagoo.wificontrolsys.redis.JedisClient;

/** 
 *     JedisClientCluster.java 
 *     :redis   
 * @author YCJ     E-mail: [email protected]
 * @date     :2017 12 24    12:54:15 
 * @version 1.0 
 */
public class JedisClientCluster implements JedisClient {

    @Autowired
    private JedisCluster jedisCluster;

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

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

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

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

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

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

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

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

    @Override
    public long hdel(String hkey, String key) {
        return jedisCluster.hdel(hkey,key);
    }

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

}