java操作Redis
Java 访问 redis
Java 操作 redis
string(字符串)
hash(哈希)
list(列表)
set(集合)
zset(sorted set:有序集合)
package com.cjh;
import redis.clients.jedis.Jedis;
/
- @author
- @site
- @company
- @create 2019-09-18 11:21
- 讲的是 java 代码操作 redis
- 链接 redis
- 操作字符串
- 操作哈希
- 操作列表 list
*/
public class Dome1 {
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
Jedis jedis </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Jedis(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">192.168.198.129</span><span style="color: rgba(128, 0, 0, 1)">"</span> ,<span style="color: rgba(128, 0, 128, 1)">6379</span><span style="color: rgba(0, 0, 0, 1)">);
jedis.auth(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">123456</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
System.</span><span style="color: rgba(0, 0, 255, 1)">out</span><span style="color: rgba(0, 0, 0, 1)">.println(jedis.ping());
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">操作字符串
// jedis.set("aaa","zs");
// System.out.println(jedis.get("aaa"));
// //操作哈希
// jedis.hset("user","uname","蔡徐坤");
//
// jedis.hset("user","sex","女");
// //取一个对象
// System.out.println(jedis.hgetAll("user"));
// //取一个对象里面的一个值
// System.out.println(jedis.hget("user", "uname"));
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">操作列表</span>
jedis.lpush("hobby","唱","跳","rap");
System.out.println(jedis.lpop("hobby"));
System.out.println(jedis.lpop("hobby"));
System.out.println(jedis.rpop("hobby"));}
}
package com.cjh;
import redis.clients.jedis.Jedis;
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.Map;
/
-
@author
-
@site
-
@company
-
@create 2019-09-18 11:46
*/
@WebServlet("/getData")
public class DomeServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//首页第一次是读数据库,后面是读缓存(在没有增删改的情况下)
Jedis jedis = new Jedis("192.168.198.129" ,6379);
jedis.auth("123456");
//从缓存里面获取用户信息
Map<String,String> currentUser= jedis.hgetAll("currentUser");
if(currentUser != null && currentUser.size()>0){
req.setAttribute("msg","从缓存里面拿数据");
req.setAttribute("currentUser",currentUser);
}
else{
//第一次登陆访问的是数据库
req.setAttribute("msg","从数据库里面拿数据");
String uname = "蔡徐坤";
String upwd = "123456";
//把数据中对应对象存储到缓存中
jedis.hset("currentUser","uanem","蔡徐坤");
jedis.hset("currentUser","upwd","123456");
//能获取到值是上面已经把数据存储到缓存中
currentUser= jedis.hgetAll("currentUser");
req.setAttribute("currentUser",currentUser);
}
req.getRequestDispatcher("/home.jsp").forward(req,resp);
}
}
项目运用 Redis
优化 luncec 和网页静态化代码:
pom 依赖:
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
只需要修改请求层里面的代码
package com.javaxl.blog.web;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Paths;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.highlight.Formatter;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.struts2.ServletActionContext;
import com.alibaba.fastjson.JSON;
import com.javaxl.blog.dao.BlogDao;
import com.javaxl.blog.util.JsonUtils;
import com.javaxl.blog.util.PropertiesUtil;
import com.javaxl.blog.util.StringUtils;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import redis.clients.jedis.Jedis;
/
- 专门将网页静态化的类,这里不涉及到 lucene
- @author Administrator
*/
public class FreemarkerBlogAction {
private String title;
private String bid;
private BlogDao blogDao = new BlogDao();
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getTitle() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> title;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setTitle(String title) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.title =<span style="color: rgba(0, 0, 0, 1)"> title;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getBid() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> bid;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setBid(String bid) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.bid =<span style="color: rgba(0, 0, 0, 1)"> bid;
}
</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> String add=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">192.168.198.129</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* 连接redis
* @return
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> Jedis Jedis() {
Jedis jedis </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Jedis(add, <span style="color: rgba(128, 0, 128, 1)">6379</span><span style="color: rgba(0, 0, 0, 1)">);
jedis.auth(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">123456</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> jedis;
}
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* 查询博客
* @return
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String list() {
</span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
HttpServletRequest request </span>=<span style="color: rgba(0, 0, 0, 1)"> ServletActionContext.getRequest();
Jedis jedis </span>=<span style="color: rgba(0, 0, 0, 1)"> Jedis();
</span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (StringUtils.isBlank(title)) {
</span><span style="color: rgba(0, 0, 255, 1)">if</span>(jedis.<span style="color: rgba(0, 0, 255, 1)">get</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">blogList</span><span style="color: rgba(128, 0, 0, 1)">"</span>)!=<span style="color: rgba(0, 0, 255, 1)">null</span> && jedis.<span style="color: rgba(0, 0, 255, 1)">get</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">blogList</span><span style="color: rgba(128, 0, 0, 1)">"</span>).length()><span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">) {
System.</span><span style="color: rgba(0, 0, 255, 1)">out</span>.println(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">从缓存里面拿数据</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
request.setAttribute(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">blogList</span><span style="color: rgba(128, 0, 0, 1)">"</span>, JSON.parse(jedis.<span style="color: rgba(0, 0, 255, 1)">get</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">blogList</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)));
}
</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
System.</span><span style="color: rgba(0, 0, 255, 1)">out</span>.println(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">从数据里拿数据</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">从数据库里面拿数据</span>
List<Map<String, Object>> list = <span style="color: rgba(0, 0, 255, 1)">this</span>.blogDao.freemarker_list(title, <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">放人Redis缓存</span>
jedis.<span style="color: rgba(0, 0, 255, 1)">set</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">blogList</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">, JSON.toJSONString(list));
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">传到jsp页面</span>
request.setAttribute(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">blogList</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">, list);
}
} </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
SmartChineseAnalyzer analyzer </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SmartChineseAnalyzer();
IndexReader indexReader </span>=<span style="color: rgba(0, 0, 0, 1)"> DirectoryReader
.open(FSDirectory.open(Paths.</span><span style="color: rgba(0, 0, 255, 1)">get</span>(PropertiesUtil.getValue(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">indexPath</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">))));
IndexSearcher searcher </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> IndexSearcher(indexReader);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 拿一句话到索引目中的索引文件中的词库进行关键词碰撞</span>
Query query = <span style="color: rgba(0, 0, 255, 1)">new</span> QueryParser(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">title</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">, analyzer).parse(title);
TopDocs topDocs </span>= searcher.search(query, <span style="color: rgba(128, 0, 128, 1)">100</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将碰撞出来的关键词给点亮</span>
QueryScorer queryScorer = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> QueryScorer(query);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 以什么形式点亮关键词</span>
Formatter formatter = <span style="color: rgba(0, 0, 255, 1)">new</span> SimpleHTMLFormatter(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)"><span style='color:red;'><b></span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)"></span></b></span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
Highlighter highlighter </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Highlighter(formatter, queryScorer);
List</span><Map<String, Object>> blogList = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList<><span style="color: rgba(0, 0, 0, 1)">();
Map</span><String, Object> map = <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
ScoreDoc[] scoreDocs </span>=<span style="color: rgba(0, 0, 0, 1)"> topDocs.scoreDocs;
</span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (ScoreDoc scoreDoc : scoreDocs) {
map </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> HashMap<><span style="color: rgba(0, 0, 0, 1)">();
Document doc </span>=<span style="color: rgba(0, 0, 0, 1)"> searcher.doc(scoreDoc.doc);
map.put(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">bid</span><span style="color: rgba(128, 0, 0, 1)">"</span>, doc.<span style="color: rgba(0, 0, 255, 1)">get</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">bid</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">));
map.put(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">summary</span><span style="color: rgba(128, 0, 0, 1)">"</span>, doc.<span style="color: rgba(0, 0, 255, 1)">get</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">summary</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">));
String titleHighlighter </span>= doc.<span style="color: rgba(0, 0, 255, 1)">get</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">title</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (StringUtils.isNotBlank(titleHighlighter)) {
titleHighlighter </span>= highlighter.getBestFragment(analyzer, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">title</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">, titleHighlighter);
}
map.put(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">title</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">, titleHighlighter);
blogList.add(map);
}
indexReader.close();
request.setAttribute(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">blogList</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">, blogList);
}
} </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> TODO: handle exception</span>
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">blogList</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String show() {
HttpServletRequest request </span>=<span style="color: rgba(0, 0, 0, 1)"> ServletActionContext.getRequest();
</span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
Map</span><String, Object> blog = <span style="color: rgba(0, 0, 255, 1)">this</span>.blogDao.freemarker_show(bid).<span style="color: rgba(0, 0, 255, 1)">get</span>(<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">);
request.setAttribute(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">blog</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">, blog);
} </span><span style="color: rgba(0, 0, 255, 1)">catch</span> (InstantiationException | IllegalAccessException |<span style="color: rgba(0, 0, 0, 1)"> SQLException e) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> TODO Auto-generated catch block</span>
e.printStackTrace();
}
return "blogDetail";
}
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* 刷新全局索引
*
* @return
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String Starter() {
IndexWriterConfig conf </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> IndexWriterConfig(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SmartChineseAnalyzer());
Directory d;
IndexWriter indexWriter </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将原先的索引文件刪除</span>
del(<span style="color: rgba(0, 0, 255, 1)">new</span> File(PropertiesUtil.getValue(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">indexPath</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)));
d </span>= FSDirectory.open(Paths.<span style="color: rgba(0, 0, 255, 1)">get</span>(PropertiesUtil.getValue(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">indexPath</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)));
indexWriter </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> IndexWriter(d, conf);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 为数据库中的所有数据构建索引</span>
List<Map<String, Object>> list = blogDao.freemarker_list(<span style="color: rgba(0, 0, 255, 1)">null</span>, <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">for</span> (Map<String, Object><span style="color: rgba(0, 0, 0, 1)"> map : list) {
Document doc </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Document();
doc.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> StringField(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">id</span><span style="color: rgba(128, 0, 0, 1)">"</span>, String.valueOf(map.<span style="color: rgba(0, 0, 255, 1)">get</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">bid</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)), Field.Store.YES));
doc.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> StringField(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">bid</span><span style="color: rgba(128, 0, 0, 1)">"</span>, String.valueOf(map.<span style="color: rgba(0, 0, 255, 1)">get</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">bid</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)), Field.Store.YES));
doc.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> TextField(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">title</span><span style="color: rgba(128, 0, 0, 1)">"</span>, (String) map.<span style="color: rgba(0, 0, 255, 1)">get</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">title</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">), Field.Store.YES));
doc.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> TextField(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">summary</span><span style="color: rgba(128, 0, 0, 1)">"</span>, (String) map.<span style="color: rgba(0, 0, 255, 1)">get</span>(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">summary</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">), Field.Store.YES));
indexWriter.addDocument(doc);
}
} </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> TODO: handle exception</span>
e.printStackTrace();
}
</span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (indexWriter != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
indexWriter.close();
}
} </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IOException e) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> TODO Auto-generated catch block</span>
e.printStackTrace();
}
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">blogList</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* 将原先的索引文件刪除
*
* @param file
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> del(File file) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> TODO Auto-generated method stub</span>
<span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (file.isDirectory()) {
String[] children </span>= file.list();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取文件夹下所有子文件夹
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 递归删除目录中的子目录下</span>
<span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = <span style="color: rgba(128, 0, 128, 1)">0</span>; i < children.length; i++<span style="color: rgba(0, 0, 0, 1)">) {
del(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> File(file, children[i]));
}
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 目录空了,进行删除</span>
file.delete();
}
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* 添加博客
*
* @return
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String add() {
HttpServletRequest request </span>=<span style="color: rgba(0, 0, 0, 1)"> ServletActionContext.getRequest();
Map parameterMap </span>=<span style="color: rgba(0, 0, 0, 1)"> request.getParameterMap();
</span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.blogDao.add(parameterMap);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">删除缓存</span>
Jedis jedis =<span style="color: rgba(0, 0, 0, 1)"> Jedis();
jedis.del(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">blogList</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取当前博客的id</span>
<span style="color: rgba(0, 0, 255, 1)">int</span> maxId = <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.blogDao.maxId();
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 添加到lucene 索引库中</span>
addIndex(maxId + <span style="color: rgba(128, 0, 0, 1)">""</span><span style="color: rgba(0, 0, 0, 1)">, parameterMap);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 进行网页静态化</span>
addStaticPage(maxId + <span style="color: rgba(128, 0, 0, 1)">""</span><span style="color: rgba(0, 0, 0, 1)">, parameterMap);
} </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> TODO: handle exception</span>
e.printStackTrace();
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">blogList</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
}
/
-
删除博客
-
@return
/
public String del() {
try {
// 数据库中删除博客
this.blogDao.del(bid);
//删除缓存
Jedis jedis = Jedis();
jedis.del("blogList");
// 删除 lucene 中对应的文档
IndexWriter indexWriter = getIndexWriter();
indexWriter.deleteDocuments(new Term("id", bid));
indexWriter.forceMergeDeletes();
indexWriter.commit();
indexWriter.close();
// 删除页面
new File("E:\y2\17、freemarker\javaxl_lunece_freemarker\src\main\webapp\freemarker\" + bid
+ ".html").delete();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return "blogList";
}
/*
-
修改前
-
@return
*/
public String perEidt() {
HttpServletRequest request = ServletActionContext.getRequest();
try {
Map<String, Object> map = this.blogDao.getBlogById(bid);
request.setAttribute("map", map);
} catch (InstantiationException | IllegalAccessException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "edit";
}
/
- 修改
- @return
*/
public String edit() {
HttpServletRequest request = ServletActionContext.getRequest();
Map parameterMap = request.getParameterMap();
try {
// 修改数据库中的值
this.blogDao.edit(request.getParameterMap());
//删除缓存
Jedis jedis = Jedis();
jedis.del("blogList");
// 修改 lucene 中的文档值
IndexWriter writer = getIndexWriter();
Document doc = new Document();
doc.add(new StringField("id", JsonUtils.getParamVal(parameterMap, "bid"), Field.Store.YES));
doc.add(new StringField("bid", JsonUtils.getParamVal(parameterMap, "bid"), Field.Store.YES));
doc.add(new TextField("title", JsonUtils.getParamVal(parameterMap, "title"), Field.Store.YES));
doc.add(new TextField("summary", JsonUtils.getParamVal(parameterMap, "summary"), Field.Store.YES));
writer.updateDocument(new Term("id", JsonUtils.getParamVal(parameterMap, "bid")), doc);
writer.close();
// 修改静态页 (相同 id 会之间覆盖)
addStaticPage(JsonUtils.getParamVal(parameterMap, "bid"), parameterMap);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return "blogList";
}
/
-
将这篇博客进行网页静态化
-
@param string
-
@param parameterMap
-
@throws IOException
*/
private void addStaticPage(String id, Map parameterMap) throws IOException {
// TODO Auto-generated method stub
IndexWriter indexWriter = getIndexWriter();
Document doc = new Document();
doc.add(new StringField("id", id, Field.Store.YES));
doc.add(new StringField("bid", id, Field.Store.YES));
doc.add(new TextField("title", JsonUtils.getParamVal(parameterMap, "title"), Field.Store.YES));
doc.add(new TextField("summary", JsonUtils.getParamVal(parameterMap, "summary"), Field.Store.YES));
indexWriter.addDocument(doc);
indexWriter.close();
}
private IndexWriter getIndexWriter() throws IOException {
// TODO Auto-generated method stub
IndexWriterConfig conf = new IndexWriterConfig(new SmartChineseAnalyzer());
Directory d = FSDirectory.open(Paths.get(PropertiesUtil.getValue("indexPath")));
return new IndexWriter(d, conf);
}
/
- 添加到 lucene 索引库中
- @param string
- @param parameterMap
- @throws IOException
- @throws TemplateException
*/
private void addIndex(String id, Map parameterMap) throws IOException, TemplateException {
// TODO Auto-generated method stub
// 1. 创建配置类
Configuration configuration = new Configuration(Configuration.getVersion());
// 2. 设置模板所在的目录
configuration.setDirectoryForTemplateLoading(
new File("E:\y2\17、freemarker\javaxl_lunece_freemarker\src\main\webapp\freemarker"));
// 3. 设置字符集
configuration.setDefaultEncoding("utf-8");
// 4. 加载模板 (这是在 刚刚设置好的 目录下面去找)
Template template = configuration.getTemplate("blogDetail.ftl");
Map map = new HashMap<>();
Map<String, Object> blog = new HashMap<>();
blog.put("bid", id);
blog.put("title", JsonUtils.getParamVal(parameterMap, "title"));
blog.put("releaseDate", new Date());
blog.put("btid", JsonUtils.getParamVal(parameterMap, "btid"));
blog.put("clickHit", JsonUtils.getParamVal(parameterMap, "clickHit"));
blog.put("content", JsonUtils.getParamVal(parameterMap, "content"));
map.put("blog", blog);
// // 6. 创建 Writer 对象
Writer out = new FileWriter(new File(
"E:\y2\17、freemarker\javaxl_lunece_freemarker\src\main\webapp\freemarker\" + id + ".html"));
// 7. 输出
template.process(map, out);
// 8. 关闭 Writer 对象
out.close();
}
}
效果:
很明显从缓存里面拿数据要比从数据库里面拿数据要快