spring MVC整合redis实例(简…

来源:互联网 发布:淘宝护肤品摄影 编辑:程序博客网 时间:2024/04/27 16:18
个人官方网站 :点击进入


第一步:pom.xml文件中//因为项目中需要用到jedis的jar
(jedis是redis的客户端 通过这个客户端我们可以操作我们的redis数据库)






spring <wbr>MVC整合redis实例(简单配置实例)泽0715新浪博客






第二步:spring的配置文件中





  spring <wbr>MVC整合redis实例(简单配置实例)泽0715新浪博客






 
可以看到我在spring的配置文件中写了一个ID为redisService的bean,它的作用就是
为我们操作reids数据库封装了一个服务,通过他 可以对redis数据库进行各种操作

以下是redisService的具体代码


————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

package com.azbb.service.Impl;
import java.io.Serializable;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.factory.annotation.Qualifier; 
importorg.springframework.data.redis.connection.jedis.JedisConnectionFactory; 

import com.azbb.util.SerializeUtil;

import redis.clients.jedis.Jedis;
public class RedisService {
 
    public void del(byte []key){  
       this.getJedis().del(key); 
   
     
    public void del(Stringkey){  
       this.getJedis().del(key); 
   
  
     
    public void set(byte []key,byte [] value,int liveTime){  
       this.set(key, value);  
       this.getJedis().expire(key, liveTime); 
   
     
    public void set(Stringkey,String value,int liveTime){  
       this.set(key, value);  
       this.getJedis().expire(key, liveTime); 
   
     
    public void set(Stringkey,String value){  
       this.getJedis().set(key, value); 
   
     
    public void set(byte []key,byte [] value){  
       this.getJedis().set(key, value); 
   
     
    public String get(Stringkey){  
       String value = this.getJedis().get(key); 
       return value;  
   
     
    public voidsetObject(Object obj,String key,int liveTime){ 
    byte[] oc =SerializeUtil.serialize(obj);
   this.getJedis().set(key.getBytes(), oc);
   this.getJedis().expire(key, liveTime);
   
    
    
     
    public voidsetObject(Object obj,String key){  
    byte[] oc =SerializeUtil.serialize(obj);
   this.getJedis().set(key.getBytes(), oc);
   
    
     
    public ObjectgetObject(String key){  
    byte[] person=this.getJedis().get(key.getBytes());
    returnSerializeUtil.unserialize(person);
    }
    
    
    
    
    
     
    public byte[] set(byte[] key){  
       return this.getJedis().get(key); 
   
    
    
    
    
    
    
  
     
    public Set keys(Stringpattern){  
       return this.getJedis().keys(pattern); 
   
  
     
    public booleanexists(String key){  
       return this.getJedis().exists(key); 
   
     
    public String flushDB(){ 
       return this.getJedis().flushDB(); 
   
     
    public long dbSize(){ 
       return this.getJedis().dbSize(); 
   
     
    public String ping(){ 
       return this.getJedis().ping(); 
   
     
    private JedisgetJedis(){  
       if(jedis == null){  
           returnjedisConnectionFactory.getShardInfo().createResource(); 
       }  
       return jedis;  
   
    private RedisService (){ 
  
   
    //操作redis客户端 
    private static Jedisjedis;  
    @Autowired 
   @Qualifier("jedisConnectionFactory")  
    privateJedisConnectionFactoryjedisConnectionFactory; 
    public voidsetJedisConnectionFactory(
JedisConnectionFactory jedisConnectionFactory) {
this.jedisConnectionFactory = jedisConnectionFactory;
}
}


序列化对象的工具类//通过这个类可以把对象序列化写入redis中

package com.azbb.util;


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

public class SerializeUtil {
public static byte[] serialize(Object object) {
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
try {
//序列化
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
byte[] bytes = baos.toByteArray();
return bytes;
} catch (Exception e) {
 
}
return null;
}
 
public static Object unserialize(byte[] bytes) {
ByteArrayInputStream bais = null;
try {
//反序列化
bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
return ois.readObject();
} catch (Exception e) {
 
}
return null;
}
}


spring 整合redis成功()
0 0