释放双眼,带上耳机,听听看~!
java操作Redis
添加redis的pom依赖
1
2
3
4
5
6
7 1<dependency>
2 <groupId>redis.clients</groupId>
3 <artifactId>jedis</artifactId>
4 <version>2.9.0</version>
5</dependency>
6
7
连接redis
1
2
3
4
5
6
7 1Jedis jedis = new Jedis("192.168.182.128", 6379); //指定Redis服务Host和port
2 jedis.auth("root");
3 String value = jedis.get("key"); //访问Redis服务
4 jedis.close(); //使用完关闭连
5 System.out.println(jedis.ping());
6
7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 1// 操作字符串
2// jedis.set("aaa","lyd");
3// System.out.println(jedis.get("aaa"));
4
5// 操作哈希
6// jedis.hset("user1","uname3","bbb");
7// jedis.hset("user1","uname4","ccc");
8
9// System.out.println(jedis.hgetAll("user1"));
10// System.out.println(jedis.hget("user1", "uname3"));
11
12// 操作列表
13// jedis.lpush("hobby","a","b","c","d","f");
14
15// lpop 从下往上弹栈 rpop 从上往下弹栈
16// System.out.println(jedis.lpop("hobby"));
17// System.out.println(jedis.rpop("hobby"));
18
19
Demo1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 1package com.xhh;
2
3import redis.clients.jedis.Jedis;
4
5import javax.servlet.ServletException;
6import javax.servlet.annotation.WebServlet;
7import javax.servlet.http.HttpServlet;
8import javax.servlet.http.HttpServletRequest;
9import javax.servlet.http.HttpServletResponse;
10import java.io.IOException;
11import java.util.Map;
12
13/**
14 * @author 林耀东
15 * @site www.baidu.com
16 * @company
17 * @create 2019-11-13 16:41
18 */
19@WebServlet("/getDate")
20public class DemoServlet extends HttpServlet {
21 @Override
22 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
23 doPost(req,resp);
24 }
25
26 @Override
27 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
28// 首页第一次是读取数据库,后面读取缓存(在没有增删改的情况)
29// 首先从缓存中去拿数据
30 Jedis jedis = new Jedis("192.168.182.128", 6379);
31 jedis.auth("root");
32// 从缓存中获取当前登录的用户信息
33 Map<String, String> currentUser = jedis.hgetAll("currentUser");
34 if(currentUser !=null && currentUser.size()>0){
35 req.setAttribute("msg","从缓存中拿到数据");
36 }
37 else{
38// 第一次登录,第一次访问首页数据
39 req.setAttribute("msg","是从缓存中拿到数据");
40 String uname = "zhangsan";
41 String upass = "123456";
42
43// 接下来的把数据中对应的对象存储到保存缓存中
44 jedis.hset("currentUser","uname3","zhangsan");
45 jedis.hset("currentUser","uname3","123456");
46// 此时能获取到值原因是上面已经讲数据存储到缓存中
47 currentUser = jedis.hgetAll("currentUser");
48 req.setAttribute("currentUser",currentUser);
49 }
50 req.getRequestDispatcher("/home.jsp").forward(req,resp);
51
52 }
53
54}
55
56
jsp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 1<%--
2 Created by IntelliJ IDEA.
3 User: a
4 Date: 2019-11-13
5 Time: 16:42
6 To change this template use File | Settings | File Templates.
7--%>
8<%@ page contentType="text/html;charset=UTF-8" language="java" %>
9<%@page isELIgnored= "false"%>
10<html>
11<head>
12 <title>Title</title>
13</head>
14<body>
15博客首页
16拿去数据的方式:${msg}<br>
17拿到的数据${currentUser}
18</body>
19</html>
20
21
第一次访问时是直接读数据库,后面都是进缓存拿数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 1public String list() {
2 try {
3 HttpServletRequest request = ServletActionContext.getRequest();
4 if (StringUtils.isBlank(title)) {
5
6 String blogListJsonStr = jedis.get("blogList");
7 //如果redis中有值
8 if(blogListJsonStr!= null && blogListJsonStr.length() > 0) {
9 // 使用redis
10 request.setAttribute("blogList", JSON.parse(blogListJsonStr));
11 }else {
12 //使用数据库
13 //从数据库中查询数据
14 List<Map<String, Object>> blogList = this.blogDao.freemarker_list(title, null);
15 //放进缓存
16 jedis.set("blogList", JSON.toJSONString(blogList));
17 //存作用域
18 request.setAttribute("blogList", blogList);
19 }
20 }else {
21 Directory directory = LuceneUtil.getDirectory(PropertiesUtil.getValue("indexPath"));
22 DirectoryReader reader = LuceneUtil.getDirectoryReader(directory);
23 IndexSearcher searcher = LuceneUtil.getIndexSearcher(reader);
24 SmartChineseAnalyzer analyzer = new SmartChineseAnalyzer();
25// 拿一句话到索引目中的索引文件中的词库进行关键词碰撞
26 Query query = new QueryParser("title", analyzer).parse(title);
27 Highlighter highlighter = LuceneUtil.getHighlighter(query, "title");
28
29 TopDocs topDocs = searcher.search(query , 100);
30 //处理得分命中的文档
31 List<Map<String, Object>> blogList = new ArrayList<>();
32 Map<String, Object> map = null;
33 ScoreDoc[] scoreDocs = topDocs.scoreDocs;
34 for (ScoreDoc scoreDoc : scoreDocs) {
35 map = new HashMap<>();
36 Document doc = searcher.doc(scoreDoc.doc);
37 map.put("id", doc.get("id"));
38 String titleHighlighter = doc.get("title");
39 if(StringUtils.isNotBlank(titleHighlighter)) {
40 titleHighlighter = highlighter.getBestFragment(analyzer, "title", titleHighlighter);
41 }
42 map.put("title", titleHighlighter);
43 map.put("url", doc.get("url"));
44 blogList.add(map);
45 }
46
47 request.setAttribute("blogList", blogList);
48 }
49 } catch (Exception e) {
50 e.printStackTrace();
51 }
52 return "blogList";
53 }
54
55
实现增删查改:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 1public String add() {
2 HttpServletRequest request = ServletActionContext.getRequest();
3 Map<String,String[]> parameterMap = request.getParameterMap();
4 try {
5 //清空缓存
6 jedis.del("blogList");
7 //加入数据库
8 this.blogDao.save(parameterMap);
9 } catch (Exception e) {
10 // TODO: handle exception
11 e.printStackTrace();
12 }
13 return "blogList";
14 }
15
16 public String del() {
17 try {
18 HttpServletRequest request = ServletActionContext.getRequest();
19 //清空缓存
20 jedis.del("blogList");
21 // 数据库中删除博客
22 this.blogDao.del(request.getParameterMap());
23
24 } catch (Exception e) {
25 // TODO: handle exception
26 e.printStackTrace();
27 }
28 return "blogList";
29 }
30public String edit() {
31 HttpServletRequest request = ServletActionContext.getRequest();
32 Map parameterMap = request.getParameterMap();
33 try {
34 // 修改数据库中的值
35 this.blogDao.edit(request.getParameterMap());
36 //清空缓存
37 jedis.del("blogList");
38 // 修改lucene中的文档值
39 IndexWriter writer = getIndexWriter();
40 Document doc = new Document();
41 doc.add(new StringField("id", JsonUtils.getParamVal(parameterMap, "bid"), Field.Store.YES));
42 doc.add(new StringField("bid", JsonUtils.getParamVal(parameterMap, "bid"), Field.Store.YES));
43 doc.add(new TextField("title", JsonUtils.getParamVal(parameterMap, "title"), Field.Store.YES));
44 doc.add(new TextField("summary", JsonUtils.getParamVal(parameterMap, "summary"), Field.Store.YES));
45 writer.updateDocument(new Term("id", JsonUtils.getParamVal(parameterMap, "bid")), doc);
46 writer.close();
47 // 修改静态页(相同id会之间覆盖)
48 addStaticPage(JsonUtils.getParamVal(parameterMap, "bid"), parameterMap);
49 } catch (Exception e) {
50 // TODO: handle exception
51 e.printStackTrace();
52 }
53 return "blogList";
54 }
55
56
57