将对象序列化存到redis缓存中

当前spring框架提供了完整的redis缓存的注解支持,只要在方法前面加上注解@Cacheable
直接去访问这个方法就会自动获取缓存。但是
会存在一种这样的需求就是你不想去访问这方法
就可以当前缓存中数据。 这个时候你可以自
己写方法去获取当前缓存key值的对应缓存的
数据。一下是我写的手动去获取我存在缓存中的
hashMap和删除hashMap中的数据,其中
hashmap被封装在类中,该类需要实现序列化接口,
再将该类序列化保存到缓存中。

package com.bisa.hkshop.zj.component;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.stereotype.Component;

import com.bisa.health.appserver.utils.CacheUtity;
import com.bisa.hkshop.zj.basic.utility.BaseDelayed;
import com.bisa.hkshop.zj.basic.utility.DelayOrderDto;

@Component
public class OrderRedisImp implements IOrderRedis{

    @Autowired
    private RedisTemplate redisTemp;

    //获取所有redis中所有的order
    @Override
    public HashMap> getOrderRedis() {//对象换成你自己
        DelayOrderDto order;
        if(redisTemp.hasKey("order")){
                    order = (DelayOrderDto) redisTemp.execute(new RedisCallback() {
                    public Object doInRedis(RedisConnection connection) throws DataAccessException {
                        String sms_key = "order";
                        byte[] value = connection.get(sms_key.getBytes());
                        if(value.length>0){
                            return CacheUtity.toObject(value);
                        }else{
                            return "";
                        }
                    }
                });
          return  order.getDelaylist();
        }
        return null;
    }


    //添加到缓存
    public void addOrderRedis(BaseDelayed delayed) { //对象是你自己的
        /**
         * 将order存到redis缓存
         */
        //对象换成你自己
        HashMap> delayList = this.getOrderRedis();
        if(delayList==null){
            delayList = new HashMap>();
        }
        delayList.put(delayed.getValue(), delayed);

        DelayOrderDto order = new DelayOrderDto();
        order.setDelaylist(delayList); //存到封装好的map中

        redisTemp.execute(new RedisCallback() {
            public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
                String sms_key = "order";
                connection.setEx(sms_key.getBytes(), 3600, CacheUtity.toByteArray(order));
                return true;
            }
        });


    }
        //删除缓存中某个order
    public void delOrderRedis(BaseDelayed delayed) {
        /*
         * 将delay从redis中的删除
         */
        HashMap> delayList = this.getOrderRedis();
        delayList.remove(delayed.getValue());
        DelayOrderDto order = new DelayOrderDto();
        order.setDelaylist(delayList); //存到封装好的HashMap中
        redisTemp.execute(new RedisCallback() {
            public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
                String sms_key = "order";
                connection.setEx(sms_key.getBytes(), 3600, CacheUtity.toByteArray(order));
                return true;
            }
        });

    }



} 
  

下面是关于序列化的两个方法:

package com.bisa.health.appserver.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class CacheUtity {
    //将对象序列化
    static public 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;
    }
//反序列化
    static public  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;
    }
}

你可能感兴趣的:(将对象序列化存到redis缓存中)