1.redis存储是:key value格式的数据,其中key都是字符串,value有5种不同的数据结构
value的数据结构:
1.字符串类型:string
2.哈希类型 hash :map格式
3.列表类型 list:linkedlist格式。支持重复元素
4.集合类型 set:不允许重复元素
5.有序集合类型 sortset:不允许重复元素,且元素有顺序
2.字符串类型 string
1.存储:set key value
2.获取:get key
3.删除:del key
3.哈希类型 hash:
1.存储:hset key field value
2.获取:hget key field
3.删除:hdel key field
jeids的操作
string数据结构操作
//1.获取连接
Jedis jedis = new Jedis("localhost", 6379);
//2.操
//存储
jedis.set("username","zhangsan");
//获取
String username = jedis.get("username");
System.out.println(username);
//可以使用setx方法存储指定国旗时间的key value
jedis.setex("active",20,"hehe");//将active hehe键值对存入redis,并且20秒后自动删除键值对
//3.关闭连接
jedis.close();
hash数据结构操作
//1.获取连接
Jedis jedis = new Jedis("localhost", 6379);
//2.操作
//存储
jedis.hset("user","name","lisi");
jedis.hset("user","age","23");
jedis.hset("user","gender","male");
//获取
String name = jedis.hget("user", "name");
System.out.println(name);
//获取hash的所有的map的数据
Map<String, String> user = jedis.hgetAll("user");
Set<String> set = user.keySet();
for (String key : set) {
String value = user.get(key);
System.out.println(key +":"+value);
}
//3.关闭连接
jedis.close();
list数据结构操作
//1.获取连接
Jedis jedis = new Jedis("localhost", 6379);
//2.操作
//存储
jedis.lpush("mylist","a","b","c");//从左边存 cba
jedis.rpush("mylist","a","b","c");//从右边存 abc
//获取
List<String> mylist = jedis.lrange("mylist", 0, -1);
System.out.println(mylist);
//list弹出
String element = jedis.lpop("mylist");
System.out.println(element);
String element1 = jedis.rpop("mylist");
System.out.println(element1);
List<String> mylist2 = jedis.lrange("mylist", 0, -1);
System.out.println(mylist2);
//3.关闭连接
jedis.close();
set和sortedset数据结构操作
//1.获取连接
Jedis jedis = new Jedis("localhost", 6379);
//2.操作
//存储 数字的作用是输出按从小到大排列
jedis.zadd("mysorted",3,"亚瑟");
jedis.zadd("mysorted",30,"后羿");
jedis.zadd("mysorted",25,"孙悟空");
//获取
Set<String> mysorted = jedis.zrange("mysorted", 0, -1);
System.out.println(mysorted);
//3.关闭连接
jedis.close();
jedis连接池:
使用:1.创建jedispool连接池对象
2.调用方法getResource()方法获取jedis连接
//创建配置对象
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(50);//最大活动对象
config.setMaxIdle(10);//最大保持的连接数
//1.创建对象
JedisPool jedisPool = new JedisPool();
//2.获取连接
Jedis jedis = jedisPool.getResource();
//3.使用
jedis.set("heheh","hahah");
//3.关闭连接
jedis.close();
案例:
1.提供一个页面,是一个下拉框,其中是省份
2.页面加载完成,发送ajax请求,加载省份
优化:使用redis缓存一些不经常发生变化的数据
Dao部分
dao接口类
package cn.itcast.dao;
import cn.itcast.domain.Province;
import java.util.List;
public interface ProvinceDao {
public List<Province> findAll();
}
dao实现类
package cn.itcast.dao.impl;
import cn.itcast.dao.ProvinceDao;
import cn.itcast.domain.Province;
import cn.itcast.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
public class ProvinceDaoImpl implements ProvinceDao {
//1.声明成员变量
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
@Override
public List<Province> findAll() {
//2.定义sql
String sql = "select * from province";
//3.执行sql
List<Province> list = template.query(sql, new BeanPropertyRowMapper<Province>(Province.class));
return list;
}
}
Service部分
service接口类
package cn.itcast.service;
import cn.itcast.domain.Province;
import java.util.List;
public interface ProvinceService {
public List<Province> findAll();
public String findAllJson();
}
service实现类
package cn.itcast.service.impl;
import cn.itcast.dao.ProvinceDao;
import cn.itcast.dao.impl.ProvinceDaoImpl;
import cn.itcast.domain.Province;
import cn.itcast.service.ProvinceService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.omg.CORBA.PRIVATE_MEMBER;
import redis.clients.jedis.Jedis;
import java.util.List;
public class ProvinceServiceImpl implements ProvinceService {
//声明dao
private ProvinceDao dao = new ProvinceDaoImpl();
@Override
public List<Province> findAll() {
return dao.findAll();
}
@Override
public String findAllJson() {
//先从redis查询数据
//1.获取连接
Jedis jedis = new Jedis("localhost", 6379);
//2.操作
//获取
String province_json = jedis.get("province");
//判断province_json是否是null
if (province_json == null || province_json.length() == 0){
//redis没数据,从数据库查询
System.out.println("redis没有数据,查询数据库。。。。");
//数据查询
List<Province> ps = dao.findAll();
//将list序列化为json
ObjectMapper mapper = new ObjectMapper();
try {
province_json = mapper.writeValueAsString(ps);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
//将json数据存入数据库中
jedis.set("province",province_json);
//归还连接
jedis.close();
}else{
System.out.println("redis有数据,查询缓存。。。。");
}
return province_json;
}
}
Servlet部分
package cn.itcast.web.servlet;
import cn.itcast.domain.Province;
import cn.itcast.service.ProvinceService;
import cn.itcast.service.impl.ProvinceServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/provinceServlet")
public class ProvinceServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// //调用service
// ProvinceService service = new ProvinceServiceImpl();
// List<Province> list = service.findAll();
// //序列化list为json
// ObjectMapper mapper = new ObjectMapper();
// String json = mapper.writeValueAsString(list);
//调用service查询
ProvinceServiceImpl service = new ProvinceServiceImpl();
String json = service.findAllJson();
System.out.println(json);
//响应结果
response.setContentType("application/json;charset=gbk");
response.getWriter().write(json);
}
}
Html部分
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/jquery-3.3.1.js"></script>
<script>
$(function () {
//发送ajax请求
$.get("provinceServlet",{},function (data) {
//获取select
var province = $("#province");
//遍历json
$(data).each(function () {
//创建option标签,append追加option
var option = "<option name='"+this.id+"'>"+this.name+"</option>";
province.append(option)
})
})
})
</script>
</head>
<body>
<select name="" id="province">
<option value="">--请选择省份--</option>
</select>
</body>
</html>