赞
踩
创建对象并以hash数据类型保存到redis缓存
package com.server;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pojo.Student;
@Service
public class StudentServer {
@Autowired
private RedisTemplate redisTemplate;//redisTemplate操作redis
public void setmap(){
Student stu=new Student();
stu.setStuid(“1001”);
stu.setStuname(“hashtest”);//生成一个Student对象
stu.setStugender(“man”);
//标志map的键、标志value的key、value
HashOperations<String, String, String> map=redisTemplate.opsForHash();
//向键名为stu.getStuid的map对象存储key-value对
map.put(stu.getStuid, "name", stu.getStuname);
map.put(stu.getStuid, "gender", stu.getStugender);
//设置100 seconds存活时间
redisTemplate.expire(stu.getStuid, 100, TimeUnit.SECONDS);
}
运行之后:
获取redis缓存中的map数据并输出到myeclipse控制台
public void getmao(){
long starttime=System.currentTimeMillis();
//entires() 从redis中获取map数据
Map stumap=redisTemplate.boundHashOps("1001").entries();
System.out.println("map对象:"+stumap);
System.out.println(stumap.get("name"));
long endtime=System.currentTimeMillis();
System.out.println("运行时间:"+( endtime-starttime)+"ms");
}
运行结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。