spring 整合jedis

 刚开始整合的是redis,连不上,试了下jedis,控制台报错,host没setter,看源码,JedisPool没host字段,只能通过构造注入,用的ssm框架。

 我连的Windows的redis,还加了密码。

#redis.properties
redis.maxIdle=30
redis.minIdle=10
redis.maxWaitMillis=3000
redis.testOnBorrow=true
redis.maxTotal=50
redis.host=127.0.0.1
redis.port=6379
redis.password=zxc
#default 0 ??
redis.database=0
redis.timeout=3600

 spring-redis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:p="http://www.springframework.org/schema/p"
          xmlns:tx="http://www.springframework.org/schema/tx"
          xmlns:context="http://www.springframework.org/schema/context"
          xsi:schemaLocation="
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:property-placeholder location="classpath:redis.properties"/>
    <!--设置数据池 jedis-->
    <bean id="jedisPoolConfig"  class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}"></property>
        <property name="minIdle" value="${redis.minIdle}"></property>
        <property name="maxTotal" value="${redis.maxTotal}"></property>
        <property name="maxWaitMillis" value="${redis.maxWaitMillis}"></property>
        <property name="testOnBorrow" value="${redis.testOnBorrow}"></property>
    </bean>
    <!--连接jedis--><!-- org.springframework.data.redis.connection.jedis.JedisConnectionFactory -->
    <!-- <bean id="jedisPool" class="redis.clients.jedis.JedisPool" scope="singleton" >
        <property name="host" value="${redis.host}"></property>
        <property name="port" value="${redis.port}"></property>
        <property name="password" value="${redis.password}"></property>
        <property name="poolConfig" ref="jedisPoolConfig"></property>
        <property name="timeout" value="${redis.timeout}"></property>
    </bean> -->
    <!--连接jedis-->
    <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
        <constructor-arg name="poolConfig" ref="jedisPoolConfig" />
        <constructor-arg name="host" value="${redis.host}" />
        <constructor-arg name="port" value="${redis.port}" type="int" />
        <constructor-arg name="timeout" value="${redis.timeout}" type="int" />
        <constructor-arg name="password" value="${redis.password}"/>
        <!-- <constructor-arg name="database" value="${redis.database}" type="int" />
        <constructor-arg name="clientName" value="${redis.clientName}"/> -->
   </bean>
</beans>
 redis接口

public interface RedisUtil<H,K,V,O> {
    //增
    public void insertString(K key,V value);
    public void insertObject(K key,O object);
    public void insertList(K key,List<O> objs);
    //查
    public String selectString(K key);
    public O selectObject(K key);
    public List<O> selectList(K key);
}
 redis接口实现

public interface RedisUtil<H,K,V,O> {
    //增
    public void insertString(K key,V value);
    public void insertObject(K key,O object);
    public void insertList(K key,List<O> objs);
	//查
    public String selectString(K key);
    public O selectObject(K key);
    public List<O> selectList(K key);
}

@Service("redisUtil")
public class StudentRedis implements RedisUtil<String,String,String,Student>{
 @Autowired
 private JedisPool jedisPool;

	@Override
	public void insertString(String key, String value) {
		Jedis jedis = jedisPool.getResource();
		String str = jedis.set(key, value);
		jedis.close();
	}

	@Override
	public String selectString(String key) {
		Jedis jedis = jedisPool.getResource();
		String str = jedis.get(key);
		jedis.close();
		return str;
	}

	@Override
	public void insertObject(String key, Student student) {
		Jedis jedis = jedisPool.getResource();
		jedis.set(key.getBytes(), serialize(student));
//		设置有效时间
		jedis.expire(key, CommonUtil.outTime);
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(jedis.ttl(key));
		jedis.close();
	}

	@Override
	public Student selectObject(String key) {
		Jedis jedis = jedisPool.getResource();
		byte[] bytes=jedis.get(key.getBytes());
		Student student=null;
		if(bytes!=null){
			Object obj=unSerizlize(bytes);
			if(obj instanceof Student){
				student=(Student) obj;
			}
		}
        jedis.close();
		return student;
	}

	@Override
	public void insertList(String key, List<Student> students) {
		Jedis jedis = jedisPool.getResource();
		jedis.set(key.getBytes(), serialize(students));
		jedis.close();
	}

	@Override
	public List<Student> selectList(String key) {
		Jedis jedis = jedisPool.getResource();
		byte[] bytes=jedis.get(key.getBytes());
		List<Student> students=null;
		if(bytes!=null){
			Object obj=unSerizlize(bytes);
			if(obj instanceof List){
				students=(List<Student>) obj;
			}
		}
        jedis.close();
		return students;
	}
	
	//序列化 
    public static byte [] serialize(Object obj){
        ObjectOutputStream oos=null;
        ByteArrayOutputStream baos=null;
        try {
            baos=new ByteArrayOutputStream();
            oos=new ObjectOutputStream(baos);
            oos.writeObject(obj);
            byte[] bytes=baos.toByteArray();
            return bytes;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    //反序列化
    public static Object unSerizlize(byte[] byt){
        ObjectInputStream ois=null;
        ByteArrayInputStream bais=null;
        bais=new ByteArrayInputStream(byt);
        try {
            ois=new ObjectInputStream(bais);
            Object obj=ois.readObject();
            return obj;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
 测试
//     1
		String str = sri.selectString("stu");
		if(str==null){
			sri.insertString("stu", "s1");
		}
		
//		2
		String str="stu3";
		Student student = sri.selectObject(str);
		if(student==null){
			student = ssi.sByStuNo(3);
			sri.insertObject(str, student);
		}
		System.out.println(student);
		
//		3
		String str="students";
		List<Student> students = sri.selectList(str);
		if(students==null){
			students = ssi.sStudents();
			sri.insertList(str, students);
		}




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值