1.Redis
这就不必哆嗦了,Redis 支持丰富的数据类型,String ,List,Sets ,Sorted Sets,Hashes,这就可以看出Java 操作Redis就要针对各种类型都有自己的操作。这里自己学习的是Spring-data-redis 中基于Jedis connection的不过在RedisTemplate 中 可不管这些,全部都是 connect 的操作,所以 我想说,记得引Jedis的Jar包,而且还要是2.0以上的哦
2.安装Redis
这也不罗嗦了,这里简单说下Redis cli 中的简单操作
(1) String
SET NAME ZHANG
GET NAME
INCR ,APPEND GETRANGE,MSET,STRLENGTH.
(2) LIST
LPUSH name zhang
LPUSH name hao ni zhen shi yi ge SB
LLEN name
LREM name xx
(3)SETS
SADD name zhang
SADD name zhang
SADD name hao
SMEMBERS name
(4) Sorted Sets
ZADD name 1 zhang
ZADD name 2 hao
ZRANGE
(5)HASHes
HSET name:1 name zhang
HSET name:1 last hao
HGET name:1 name
HGETALL name:1
3.简单的用Spring-data-redis
当然 关联的Jar 包用Maven 来搞,(前段时间Maven不好使,生不如死呀,各种看错误来加Jar包),然后就是在Spring applicationContext,xml中配置了
首先:Jedis 的连接池:redis.clients.jedis.JedisPoolConfig 见仁见智
Java代码
- <!-- 配置Jedis的 缓冲池 -->
- <bean id="JedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"
- p:maxActive="32"
- p:maxIdle="6"
- p:maxWait="15000"
- p:minEvictableIdleTimeMillis="300000"
- p:numTestsPerEvictionRun="3"
- p:timeBetweenEvictionRunsMillis="60000"
- p:whenExhaustedAction="1">
- </bean>
第二,创建ConnectionFactory
Java代码
- <!-- 配置Jedis connection -->
- <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
- p:poolConfig-ref="JedisPoolConfig"
- p:hostName="192.168.137.100"
- p:port="6379"
- p:usePool="true"
- >
- </bean>
第三:创建RedisTemplate
Java代码
- <!-- 配置 redisTemplate 利用Stringserializer -->
- <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
- p:connectionFactory-ref="connectionFactory"
- >
- <property name="defaultSerializer">
- <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
- </property>
- </bean>
由于redis中存储的数据必须是经过序列化的,所以Spring-data-redis中采取了几种序列化模式,有自带的JDKblablabla,还有StringRedisSerializer,
但是Json 和XML也都是基于String类型的所以就在默认文件中使用String类型的序列化,而默认是JdkSerializationRedisSerializer.
Java代码
- public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperations<K, V> {
- private boolean exposeConnection = false;
- private boolean initialized = false;
- private boolean enableDefaultSerializer = true;
- private RedisSerializer<?> defaultSerializer = new JdkSerializationRedisSerializer();
先不管那么多了,来做点简单的实验。
1.String
Java代码
- RedisTemplate rt = (RedisTemplate) context.getBean("redisTemplate");
- rt.opsForValue().set("zhang", "hao");
- System.out.println(rt.opsForValue().get("zhang"));
2.List
说到List Redis 提供了类似于消息队列的操作,先看下接口,从名字上就可以看到这些操作的意义,我就简单实例一下。
Java代码
- public interface ListOperations<K, V> {
- List<V> range(K key, long start, long end);
- void trim(K key, long start, long end);
- Long size(K key);
- Long leftPush(K key, V value);
- Long leftPushAll(K key, V... values);
- Long leftPushIfPresent(K key, V value);
- Long leftPush(K key, V pivot, V value);
- Long rightPush(K key, V value);
- Long rightPushAll(K key, V... values);
- Long rightPushIfPresent(K key, V value);
- Long rightPush(K key, V pivot, V value);
- void set(K key, long index, V value);
- Long remove(K key, long i, Object value);
- V index(K key, long index);
- V leftPop(K key);
- V leftPop(K key, long timeout, TimeUnit unit);
- V rightPop(K key);
- V rightPop(K key, long timeout, TimeUnit unit);
- V rightPopAndLeftPush(K sourceKey, K destinationKey);
- V rightPopAndLeftPush(K sourceKey, K destinationKey, long timeout, TimeUnit unit);
- RedisOperations<K, V> getOperations();
PS.我上面贴的源码是 github上面的最新源码。我自己用的还是1.01 泪千行,我说找了半天rightpushAll 竟然找不到。所以 记得用最新的版本。
Java代码
- String KEYS="LIST:";
- rt.opsForList().rightPush(KEYS, "zhang");
- rt.opsForList().leftPush(KEYS, "HAO");
- // sort : HAO zhang
- for(int i=0;i<=rt.opsForList().size(KEYS);i++){
- System.out.println(rt.opsForList().rightPop(KEYS));
- }
3 Hashes (SETS 自己看看吧。。)
Java代码
- String KEYS="HASH:";
- rt.opsForHash().put(KEYS, "name", "zhang");
- rt.opsForHash().put(KEYS, "last", "hao");
- rt.opsForHash().put(KEYS, "wo", "ni");
- rt.opsForHash().put(KEYS, "name", "sb");
- System.out.println(rt.opsForHash().get(KEYS, "name"));
可以看到key name 被覆盖了,其中有个BooleanputIfAbsent(Hkey,HKhashKey,HVvalue) 方法,这个具体细节就自己看看吧。下面把Hashoperations 里面的方法贴下:
Java代码
- public interface HashOperations<H, HK, HV> {
- void delete(H key, Object... hashKeys);
- Boolean hasKey(H key, Object hashKey);
- HV get(H key, Object hashKey);
- List<HV> multiGet(H key, Collection<HK> hashKeys);
- Long increment(H key, HK hashKey, long delta);
- Double increment(H key, HK hashKey, double delta);
- Set<HK> keys(H key);
- Long size(H key);
- void putAll(H key, Map<? extends HK, ? extends HV> m);
- void put(H key, HK hashKey, HV value);
- Boolean putIfAbsent(H key, HK hashKey, HV value);
- List<HV> values(H key);
- Map<HK, HV> entries(H key);
- RedisOperations<H, ?> getOperations();
- }
今天就先到这里,Redis 还有很多东西,比如cache transcation 等等 看下目录结构
还有比较多东西,明天继续 再接再厉!