51
    public void testObjectCache() throws Exception {
52
        // TODO Auto-generated method stub
53
        ICache cache = CacheFactory.getCache(cacheSingleName);
54
        IpuContextData contextData = new IpuContextData();
55
        contextData.put(cacheKey, strValue);
56
        cache.put(cacheContent, contextData);
57
        contextData = (IpuContextData) cache.get(cacheContent);
58
        System.out.println("strValue"+strValue);
59
        //cache.get(cacheKey) 对应的是存放缓存的hash值,其他取到的均为值等于123456的值
60
        System.out.println("cache中获取key值"+cache.get(cacheKey));
61
        System.out.println("contextData类中获取“KEY”的值"+contextData.getData().get("KEY"));
62
        assertEquals(strValue,contextData.get(cacheKey));
63
        System.out.println("=================");
64
        System.out.println(contextData.get("KEY"));
65
        assertEquals(contextData.getData().get("KEY"),strValue);
66
        System.out.println("++++++++++++++++");
67
        assertEquals(strValue, cache.get(cacheKey));
68
        System.out.println("-------------------");
69
    }
70
    
71
    public void testObjectJsonCache() throws Exception {
72
        // TODO Auto-generated method stub
73
        ICache cache = CacheFactory.getCache(cacheSingleName);
74
        IpuContextData contextData = new IpuContextData();
75
        contextData.put(cacheKey, strValue);
76
        cache.put(cacheContent, JSON.toJSONString((contextData)));
77
        String strContextData = cache.get(cacheContent).toString();
78
//        LOGGER.debug("strContextData===" + strContextData);
79
        System.out.println("strContextData===" + strContextData);
80
        IpuContextData2 contextData2 = JSON.parseObject(strContextData, IpuContextData2.class);
81
        System.out.println("contextData2.isDirty()===" + contextData2.isDirty());
82
//        LOGGER.debug("contextData2.isDirty()===" + contextData2.isDirty());
83
        assertEquals(strValue, contextData2.get(cacheKey));
84
    }
85
}

+ 111 - 0
ipu-redis-example/src/test/java/com/ai/ipu/cache/redis/RedisClusterTest.java

@ -0,0 +1,111 @@
1
package com.ai.ipu.cache.redis;
2
3
import java.io.IOException;
4
import java.util.HashSet;
5
import java.util.Set;
6
7
import junit.framework.TestCase;
8
import redis.clients.jedis.HostAndPort;
9
import redis.clients.jedis.JedisCluster;
10
import redis.clients.jedis.JedisPoolConfig;
11
12
import com.ai.ipu.basic.log.ILogger;
13
import com.ai.ipu.basic.log.IpuLoggerFactory;
14
import com.ai.ipu.cache.CacheFactory;
15
import com.ai.ipu.cache.CacheFactory.CacheType;
16
import com.ai.ipu.cache.ICache;
17
18
/**
19
 * @author huangbo@asiainfo.com
20
 * @team IPU
21
 * @date 2017年11月6日下午4:43:03
22
 * @desc Jedis原始包集群操作使用范例
23
 */
24
public class RedisClusterTest extends TestCase{
25
	private static final transient ILogger LOGGER = IpuLoggerFactory.createLogger(RedisClusterTest.class);
26
    private String[] hosts = {
27
                             "121.42.183.206",
28
                             "121.42.183.206",
29
                             "121.42.183.206",
30
                             "121.42.183.206",
31
                             "121.42.183.206",
32
                             "121.42.183.206"
33
                             };
34
    private int[] ports = {7101,7102,7103,7104,7105,7106};
35
    
36
    @Override
37
    protected void setUp() throws Exception {
38
        // TODO Auto-generated method stub
39
        super.setUp();
40
    }
41
    
42
    public void testCluster() throws Exception{
43
        JedisCluster jedisCluster = RedisCacheFactory.getJedisCluster("data");
44
        try {
45
            long start = System.currentTimeMillis();
46
            for (int i = 0; i < 100; i++) {
47
                @SuppressWarnings("unused")
48
                String result = jedisCluster.set("x" + i, "n" + i);
49
            }
50
            long end = System.currentTimeMillis();
51
            LOGGER.debug("普通同步调用方式耗时: " + ((end - start) / 1000.0) + "秒");
52
            jedisCluster.close();
53
        }catch (IOException e) {
54
        	LOGGER.error("IOException", e);
55
        }
56
    }
57
    
58
    public void testClusterWithAuth() throws Exception{
59
        JedisCluster jedisCluster = RedisCacheFactory.getJedisCluster("ssn");
60
        
61
        try {
62
            long start = System.currentTimeMillis();
63
            for (int i = 0; i < 100; i++) {
64
                jedisCluster.set("d" + i, "d" + i);
65
            }
66
            long end = System.currentTimeMillis();
67
            LOGGER.debug("普通同步调用方式耗时: " + ((end - start) / 1000.0) + "秒");
68
            jedisCluster.close();
69
        }catch (IOException e) {
70
        	LOGGER.error("IOException", e);
71
        }
72
    }
73
    
74
    public void testClusterPipelineWithAuth(){
75
        //GenericObjectPoolConfig的使用
76
        JedisPoolConfig config = new JedisPoolConfig();
77
        Set<HostAndPort> nodes = new HashSet<HostAndPort>();
78
        for(int i=0,len=hosts.length;i<len;i++){
79
            nodes.add(new HostAndPort(hosts[i], ports[i]));
80
        }
81
        JedisCluster jedisCluster = new JedisCluster(nodes, 5000, 5000, 3, "ipu",  config);
82
        try {
83
            long start = System.currentTimeMillis();
84
            for (int i = 0; i < 100; i++) {
85
                jedisCluster.set("b" + i, "b" + i);
86
            }
87
            long end = System.currentTimeMillis();
88
            LOGGER.debug("普通同步调用方式耗时: " + ((end - start) / 1000.0) + "秒");
89
            jedisCluster.close();
90
        }catch (IOException e) {
91
        	LOGGER.error("IOException", e);
92
        }
93
    }
94
    
95
    
96
    public void testCacheFactory()
97
    {
98
        try {
99
			ICache cache = CacheFactory.getCache(CacheType.redis, "jedisCluster");
100
			boolean result = cache.put("a", "a");
101
			
102
			LOGGER.debug(Boolean.toString(result));
103
			LOGGER.debug((String)cache.get("a"));
104
			LOGGER.debug(Boolean.toString(cache.remove("a")));
105
			LOGGER.debug(Boolean.toString(cache.remove("1")));
106
			
107
		} catch (Exception e) {
108
			LOGGER.error("IOException", e);
109
		}
110
    }
111
}

+ 225 - 0
ipu-redis-example/src/test/java/com/ai/ipu/cache/redis/RedisTest.java

@ -0,0 +1,225 @@
1
package com.ai.ipu.cache.redis;
2
3
import java.util.ArrayList;
4
import java.util.Arrays;
5
import java.util.List;
6
7
import com.ai.ipu.basic.log.ILogger;
8
import com.ai.ipu.basic.log.IpuLoggerFactory;
9
10
import junit.framework.TestCase;
11
import redis.clients.jedis.Jedis;
12
import redis.clients.jedis.JedisPoolConfig;
13
import redis.clients.jedis.JedisShardInfo;
14
import redis.clients.jedis.Pipeline;
15
import redis.clients.jedis.ShardedJedis;
16
import redis.clients.jedis.ShardedJedisPipeline;
17
import redis.clients.jedis.ShardedJedisPool;
18
import redis.clients.jedis.Transaction;
19
20
/**
21
 * @author huangbo@asiainfo.com
22
 * @team IPU
23
 * @date 2018年11月6日下午4:43:44
24
 * @desc Jedis原始包单服务操作使用范例
25
 */
26
public class RedisTest extends TestCase {
27
	private static final transient ILogger LOGGER = IpuLoggerFactory.createLogger(RedisTest.class);
28
	private Jedis jedis;
29
	private ShardedJedis shardedJedis;
30
	private String host;
31
	private int port;
32
	
33
	@Override
34
	protected void setUp() throws Exception {
35
		// TODO Auto-generated method stub
36
		super.setUp();
37
		host = "47.105.160.21";
38
		port = 7001;
39
	}
40
	
41
	/**
42
	 * 一、普通同步调用方式
43
	 */
44
	public void testNormal() {
45
		jedis = new Jedis(host, port);
46
		jedis.auth("ipu@321");
47
		long start = System.currentTimeMillis();
48
		List<Object> results = new ArrayList<Object>();
49
		for (int i = 0; i < 100; i++) {
50
			String result = jedis.set("n" + i, "n" + i);
51
			results.add(result);
52
		}
53
		LOGGER.debug("results.size()==="+results.size());
54
		long end = System.currentTimeMillis();
55
		LOGGER.debug("普通同步调用方式耗时: " + ((end - start) / 1000.0) + "秒");
56
		jedis.disconnect();
57
	}
58
	
59
	/**
60
	 * 二、事物调用方式
61
	 * discard()取消事务
62
	 */
63
	public void testTrans() { 
64
		jedis = new Jedis(host, port);
65
	    jedis.auth("Ipu@321!");
66
	    long start = System.currentTimeMillis(); 
67
	    Transaction tx = jedis.multi(); 
68
	    for (int i = 0; i < 10000; i++) { 
69
	        tx.set("t" + i, "t" + i); 
70
	    } 
71
	    List<Object> results = tx.exec();
72
	    LOGGER.debug("results.size()==="+results.size());
73
	    long end = System.currentTimeMillis(); 
74
	    LOGGER.debug("事物调用方式耗时: " + ((end - start)/1000.0) + " seconds");
75
	    jedis.disconnect(); 
76
	} 
77
78
	
79
	/**
80
	 * 三、管道调用方式
81
	 */
82
	public void testPipe() {
83
		// TODO Auto-generated method stub
84
		jedis = new Jedis(host, port);
85
	    jedis.auth("Ipu@321!");
86
	    Pipeline pipeline = jedis.pipelined(); 
87
	    long start = System.currentTimeMillis(); 
88
	    for (int i = 0; i < 10000; i++) { 
89
	        pipeline.set("p" + i, "p" + i); 
90
	    } 
91
	    List<Object> results = pipeline.syncAndReturnAll(); 
92
	    LOGGER.debug("results.size()==="+results.size());
93
	    long end = System.currentTimeMillis(); 
94
	    LOGGER.debug("管道调用方式耗时: " + ((end - start)/1000.0) + " seconds");
95
	    jedis.disconnect(); 
96
	}
97
	
98
	/**
99
	 * 四、管道事务调用方式
100
	 */
101
	public void testPipeTrans() {
102
		// TODO Auto-generated method stub
103
		jedis = new Jedis(host, port);
104
	    jedis.auth("ipu"); 
105
	    long start = System.currentTimeMillis(); 
106
	    Pipeline pipeline = jedis.pipelined(); 
107
	    pipeline.multi(); 
108
	    for (int i = 0; i < 10000; i++) { 
109
	        pipeline.set("" + i, "" + i); 
110
	    } 
111
	    pipeline.exec(); 
112
	    List<Object> results = pipeline.syncAndReturnAll();
113
	    LOGGER.debug("results.size()==="+results.size());
114
	    long end = System.currentTimeMillis(); 
115
	    LOGGER.debug("管道事务调用方式耗时: " + ((end - start)/1000.0) + " seconds"); 
116
	    jedis.disconnect(); 
117
	}
118
	
119
	/**
120
	 * 五、普通分布式调用方式
121
	 * 默认hash方式存储
122
	 */
123
	public void testShardNormal() {
124
		// TODO Auto-generated method stub
125
		List<JedisShardInfo> distribut = Arrays.asList( 
126
	            new JedisShardInfo(host,6379), 
127
	            new JedisShardInfo(host,6380)); 
128
129
	    shardedJedis = new ShardedJedis(distribut);
130
	    List<Object> results = new ArrayList<Object>();
131
	    long start = System.currentTimeMillis(); 
132
	    for (int i = 0; i < 100000; i++) { 
133
	        String result = shardedJedis.set("sn" + i, "n" + i);
134
	        results.add(result);
135
	    }
136
	    LOGGER.debug("results.size()==="+results.size());
137
	    long end = System.currentTimeMillis(); 
138
	    LOGGER.debug("普通分布式调用方式耗时: " + ((end - start)/1000.0) + " seconds"); 
139
140
	    shardedJedis.disconnect(); 
141
	}
142
	
143
	/**
144
	 * 六、管道分布式调用方式
145
	 */
146
    public void testShardPipe() {
147
        // TODO Auto-generated method stub
148
        List<JedisShardInfo> shards = Arrays.asList(new JedisShardInfo(host, 6379), 
149
                new JedisShardInfo(host, 6380));
150
151
        shardedJedis = new ShardedJedis(shards);
152
        ShardedJedisPipeline pipeline = shardedJedis.pipelined();
153
        long start = System.currentTimeMillis();
154
        for (int i = 0; i < 100000; i++) {
155
            pipeline.set("sp" + i, "p" + i);
156
        }
157
        List<Object> results = pipeline.syncAndReturnAll();
158
        LOGGER.debug("results.size()===" + results.size());
159
        long end = System.currentTimeMillis();
160
        LOGGER.debug("Pipelined@Sharing SET: " + ((end - start) / 1000.0) + " seconds");
161
162
        shardedJedis.disconnect();
163
    }
164
	
165
	/**
166
	 * 七、分布式连接池同步调用方式
167
	 */
168
	public void testShardPool() {
169
		// TODO Auto-generated method stub
170
		List<JedisShardInfo> shards = Arrays.asList(
171
				new JedisShardInfo(host, 6379), 
172
				new JedisShardInfo(host, 6380));
173
		ShardedJedisPool pool = new ShardedJedisPool(new JedisPoolConfig(), shards);
174
		shardedJedis = pool.getResource();
175
176
		List<Object> results = new ArrayList<Object>();
177
		long start = System.currentTimeMillis();
178
		for (int i = 0; i < 100000; i++) {
179
			String result = shardedJedis.set("spn" + i, "n" + i);
180
			results.add(result);
181
		}
182
		
183
		LOGGER.debug("results.size()===" + results.size());
184
		long end = System.currentTimeMillis();
185
		pool.returnResource(shardedJedis);
186
		LOGGER.debug("Simple@Pool SET: " + ((end - start) / 1000.0) + " seconds");
187
188
		pool.destroy();
189
	}
190
	
191
	/**
192
	 * 八、分布式连接池异步调用方式
193
	 */
194
	public void testShardPipePool() {
195
		// TODO Auto-generated method stub
196
		List<JedisShardInfo> shards = Arrays.asList( 
197
	            new JedisShardInfo("localhost",6379), 
198
	            new JedisShardInfo("localhost",6380)); 
199
200
	    ShardedJedisPool pool = new ShardedJedisPool(new JedisPoolConfig(), shards); 
201
	    shardedJedis = pool.getResource(); 
202
	    ShardedJedisPipeline pipeline = shardedJedis.pipelined(); 
203
204
	    long start = System.currentTimeMillis(); 
205
	    for (int i = 0; i < 100000; i++) { 
206
	        pipeline.set("sppn" + i, "n" + i); 
207
	    } 
208
	    List<Object> results = pipeline.syncAndReturnAll();
209
	    LOGGER.debug("results.size()===" + results.size());
210
	    long end = System.currentTimeMillis(); 
211
	    pool.returnResource(shardedJedis); 
212
	    LOGGER.debug("Pipelined@Pool SET: " + ((end - start)/1000.0) + " seconds"); 
213
	    pool.destroy(); 
214
	}
215
	
216
	public void testGet() {
217
        jedis = new Jedis(host, port);
218
        long start = System.currentTimeMillis();
219
        jedis.keys("*");
220
        String result = jedis.get("msg");
221
        LOGGER.debug("返回结果:"+result);
222
        LOGGER.debug("普通同步调用方式耗时: " + (System.currentTimeMillis() - start) + "ms");
223
        jedis.disconnect();
224
    }
225
}

+ 53 - 0
ipu-redis-example/src/test/java/com/ai/ipu/cache/redis/context/IpuContextData.java

@ -0,0 +1,53 @@
1
package com.ai.ipu.cache.redis.context;
2
3
import java.io.Serializable;
4
import java.util.HashMap;
5
import java.util.Map;
6
7
/**
8
 * @author huangbo@asiainfo.com
9
 * @team IPU
10
 * @date 2018年11月6日下午7:54:04
11
 * @desc 上下文数据对象设计
12
 * 1.不使用java对象:单点登录时,java对象的包路径需要一致。不太合理。
13
 * 2.不继承HashMap:继承的话,Json转换时会丢失私有属性dirty。dirty和xxxxDirty均被识别成dirty
14
 */
15
public class IpuContextData implements Serializable{
16
    private static final long serialVersionUID = 183972639715505942L;
17
    private Map<String, String> data;
18
    private boolean isDirty;
19
20
	public IpuContextData() {
21
	    data = new HashMap<String, String>();
22
	}
23
	
24
	public Map<String, String> getData() {
25
		// TODO Auto-generated method stub
26
		return data;
27
	}
28
29
	public void setData(Map<String, String> data) {
30
		// TODO Auto-generated method stub
31
		this.data = data;
32
		setDirty(true);
33
	}
34
	
35
	public void put(String key, String value){
36
	    data.put(key, value);
37
		setDirty(true);
38
	}
39
	
40
	public String get(String key){
41
		return data.get(key);
42
	}
43
44
	public boolean isDirty() {
45
		// TODO Auto-generated method stub
46
		return isDirty;
47
	}
48
	
49
	public void setDirty(boolean isDirty) {
50
		// TODO Auto-generated method stub
51
		this.isDirty = isDirty;
52
	}
53
}

+ 48 - 0
ipu-redis-example/src/test/java/com/ai/ipu/cache/redis/context/IpuContextData2.java

@ -0,0 +1,48 @@
1
package com.ai.ipu.cache.redis.context;
2
3
import java.io.Serializable;
4
import java.util.HashMap;
5
import java.util.Map;
6
7
/**
8
 * 上下文数据,存储的时候会被序列化
9
 */
10
public class IpuContextData2 implements Serializable{
11
    private static final long serialVersionUID = 183972639715505942L;
12
    private Map<String, String> data;
13
    private boolean isDirty;
14
15
    public IpuContextData2() {
16
        data = new HashMap<String, String>();
17
    }
18
    
19
    public Map<String, String> getData() {
20
        // TODO Auto-generated method stub
21
        return data;
22
    }
23
24
    public void setData(Map<String, String> data) {
25
        // TODO Auto-generated method stub
26
        this.data = data;
27
        setDirty(true);
28
    }
29
    
30
    public void put(String key, String value){
31
        data.put(key, value);
32
        setDirty(true);
33
    }
34
    
35
    public String get(String key){
36
        return data.get(key);
37
    }
38
39
    public boolean isDirty() {
40
        // TODO Auto-generated method stub
41
        return isDirty;
42
    }
43
    
44
    public void setDirty(boolean isDirty) {
45
        // TODO Auto-generated method stub
46
        this.isDirty = isDirty;
47
    }
48
}

+ 47 - 0
ipu-redis-example/src/test/reources/ipu-cache.xml

@ -0,0 +1,47 @@
1
<?xml version = '1.0' encoding = 'UTF-8'?>
2
<caches>
3
	<!-- ====================redis缓存============================ -->
4
	<cache name="ssn" type="redis">
5
		<servers>
6
	        <!-- 如果不是cluster,则只使用第一个redis -->
7
	        <server ip="47.105.160.21" port="7001" />
8
	        <server ip="47.105.160.21" port="7002" />
9
	        <server ip="47.105.160.21" port="7003" />
10
	        <server ip="121.42.183.206" port="7104" />
11
	        <server ip="121.42.183.206" port="7105" />
12
	        <server ip="121.42.183.206" port="7106" />
13
	    </servers>
14
		<!-- 客户端类型:Jedis,JedisCluster -->
15
	    <config name="clientType" value="JedisCluster"/>
16
	    <!-- 访问redis的密码,可以为空 -->
17
	    <config name="auth" value="ipu@321"/>
18
	    <!-- redis池的可用连接实例的最大数目,缺省为8 -->
19
	    <config name="poolSize" value="10"/>
20
	    <!-- redis池最多有多少个状态为idle(空闲的)的jedis实例,缺省为8,空闲连接大于这个数会进行回收 -->
21
	    <config name="maxIdle"/>
22
	    <!-- 最小空闲数,空闲连接小于这个数会建立新的连接,缺省为0 -->
23
	    <config name="minIdle"/>
24
	    <!-- 等待Response超时时间,默认5000ms -->
25
	    <config name="soTimeout"/>
26
	    <!-- 连接Redis Server超时时间,默认5000ms -->
27
	    <config name="connTimeout"/>
28
	    <!-- 出现异常最大重试次数 -->
29
	    <config name="maxAttempts"/>
30
	</cache>
31
	
32
	<cache name="data" type="redis">
33
		<servers>
34
	        <!-- 如果不是cluster,则只使用第一个redis -->
35
	        <server ip="47.105.160.21" port="7001" />
36
	    </servers>
37
	    <config name="auth" value="ipu@321"/>
38
	</cache>
39
	<cache name="single" type="redis">
40
		<servers>
41
	        <!-- 如果不是cluster,则只使用第一个redis -->
42
	        <server ip="47.105.160.21" port="7101" />
43
	    </servers>
44
	    <config name="auth" value="ipu@321"/>
45
	</cache>
46
	<!-- ================================================ -->
47
</caches>

+ 12 - 0
ipu-redis-example/src/test/reources/memcache.xml

@ -0,0 +1,12 @@
1
<?xml version = '1.0' encoding = 'UTF-8'?>
2
3
<memcache>
4
	<default-datacenter>center1</default-datacenter>
5
	<datacenter name="center1">
6
		<cluster name="wade_cache">
7
			<heartbeat-second>5</heartbeat-second>
8
			<pool-size>2</pool-size>
9
			<address master="47.105.160.21:11211" />
10
		</cluster>
11
	</datacenter>
12
</memcache>

simple-example - Nuosi Git Service

开源代码的简单范例

huangbo e1560a527f 过滤idea多余的文件和目录 %!s(int64=6) %!d(string=hace) años
..
src 0942f98275 springcloud-example工程初始化 %!s(int64=6) %!d(string=hace) años
.gitignore e1560a527f 过滤idea多余的文件和目录 %!s(int64=6) %!d(string=hace) años
.project 0942f98275 springcloud-example工程初始化 %!s(int64=6) %!d(string=hace) años
pom.xml 0942f98275 springcloud-example工程初始化 %!s(int64=6) %!d(string=hace) años