发表于: 2017-12-02 20:26:34

1 755


今天完成的事

【1】昨天的报红解决了。

今天完成的事

继续学习redis。

昨天用的redis并没有和spring紧密联系到一起。

下面引入的这个包根本没用到。

钻研下。

用注解的方式实现下。

package com.xiuzhen.utils;


import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;


import java.io.*;

/**
* Created by ${MIND-ZR} on 2017/12/1.
*/
public final class  RedisUtil implements Cache {

private RedisTemplate<String, Object> redisTemplate;
   private String name;
   public RedisTemplate<String, Object> getRedisTemplate() {
return redisTemplate;
   }

public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
   }

public void setName(String name) {
this.name = name;
   }


public String getName() {
// TODO Auto-generated method stub
       return this.name;
   }


public Object getNativeCache() {
// TODO Auto-generated method stub
       return this.redisTemplate;
   }


public ValueWrapper get(Object key) {
// TODO Auto-generated method stub
       System.out.println("get key");
       final String keyf = key.toString();
       Object object = null;
       object = redisTemplate.execute(new RedisCallback<Object>() {
public Object doInRedis(RedisConnection connection)
throws DataAccessException {
byte[] key = keyf.getBytes();
               byte[] value = connection.get(key);
               if (value == null) {
return null;
               }
return toObject(value);
           }
});
       return (object != null ? new SimpleValueWrapper(object) : null);
   }


public void put(Object key, Object value) {
// TODO Auto-generated method stub
       System.out.println("put key");
       final String keyf = key.toString();
       final Object valuef = value;
       final long liveTime = 86400;
       redisTemplate.execute(new RedisCallback<Long>() {
public Long doInRedis(RedisConnection connection)
throws DataAccessException {
byte[] keyb = keyf.getBytes();
               byte[] valueb = toByteArray(valuef);
               connection.set(keyb, valueb);
               if (liveTime > 0) {
connection.expire(keyb, liveTime);
               }
return 1L;
           }
});
   }

private byte[] toByteArray(Object obj) {
byte[] bytes = null;
       ByteArrayOutputStream bos = new ByteArrayOutputStream();
       try {
ObjectOutputStream oos = new ObjectOutputStream(bos);
           oos.writeObject(obj);
           oos.flush();
           bytes = bos.toByteArray();
           oos.close();
           bos.close();
       }catch (IOException ex) {
ex.printStackTrace();
       }
return bytes;
   }

private Object toObject(byte[] bytes) {
Object obj = null;
       try {
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
           ObjectInputStream ois = new ObjectInputStream(bis);
           obj = ois.readObject();
           ois.close();
           bis.close();
       } catch (IOException ex) {
ex.printStackTrace();
       } catch (ClassNotFoundException ex) {
ex.printStackTrace();
       }
return obj;
   }


public void evict(Object key) {
// TODO Auto-generated method stub
       System.out.println("del key");
       final String keyf = key.toString();
       redisTemplate.execute(new RedisCallback<Long>() {
public Long doInRedis(RedisConnection connection)
throws DataAccessException {
return connection.del(keyf.getBytes());
           }
});
   }


public void clear() {
// TODO Auto-generated method stub
       System.out.println("clear key");
       redisTemplate.execute(new RedisCallback<String>() {
public String doInRedis(RedisConnection connection)
throws DataAccessException {
connection.flushDb();
               return "ok";
           }
});
   }


public <T> T get(Object key, Class<T> type) {
// TODO Auto-generated method stub
       return null;
   }


public ValueWrapper putIfAbsent(Object key, Object value) {
// TODO Auto-generated method stub
       return null;
   }
}

配置文件

    <cache:annotation-driven/>
   <!-- spring自己的缓存管理器,这里定义了缓存位置名称 ,即注解中的value -->
   <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
       <property name="caches">
           <set>
               <!-- 这里可以配置多个redis -->
               <!-- <bean class="com.cn.util.RedisCache">
                    <property name="redisTemplate" ref="redisTemplate" />
                    <property name="name" value="default"/>
               </bean> -->
               <bean class="com.xiuzhen.utils.RedisUtil">
                   <property name="redisTemplate" ref="redisTemplate" />
                   <property name="name" value="common"/>
                   <!-- common名称要在类或方法的注解中使用 -->
               </bean>
           </set>
       </property>
   </bean>

</beans>

但是没有实现成功,一直报错。

明天搞。

遇到的问题

有一个小bug,注解redis返回的类型不对,明天调。

收获


redis下,数据库是由一个整数索引标识,而不是由一个数据库名称。默认情况下,一个客户端连接到数据库0。

默认是15个,不过配置文件里面可以改。

明天的计划

完成任务六

禅道链接。http://task.ptteng.com/zentao/task-view-14263.html


返回列表 返回列表
评论

    分享到