通过判断cookie过期方式向Memcached中添加,取出数据(Java)
应用场景:在数据驱动的web开发中,经常要重复从数据库中取出相同的数据,这种重复极大的增加了数据库负载。缓存是解决这个问题的好办法。但是ASP.NET中的虽然已经可以实现对页面局部进行缓存,但还是不够灵活。此时Memcached或许是你想要的。
一. 什么是Memcached
Memcached 是一个高性能的分布式内存 对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象 来减少读取数据库的次数,从而提供动态、数据库驱动网站的速度。
Memcached基于一 个存储键/值对的hashmap。其守护进程是用C写的,但是客户端可以用任何语言来编写(本文使用JAVA作为例子),并通过memcached协议与守护进程通信。
二. Memcached 特征
(1) 协议简单: 不使用复杂的xml格式,而是使用文本格式
(2) 基于libevent的事件处理机制
(3) 内置内存存储方式: 数据存在在内存中,所以重启机器会导致数据丢失
(4)Memcached相互不通信的分布式: Memcached 服务器之间不会进行通信,数据都是通过客户端的分布式算法存储到各个服务器中
三.Memcached的安装(windows)
下载Memcached 服务端: http://memcached.org/ (官网寻找适用的版本)
1 解压缩文件到D:\memchche\memcached-win32-1.4.4-14
2 命令行输入 D:\memchche\memcached-win32-1.4.4-14\memcached.exe -d install
3 命令行输入 D:\memchche\memcached-win32-1.4.4-14\memcached.exe -d start,该命令启动 Memcached ,默认监听端口为 11211
四.Memcached在java在java项目中的应用
(一)StaticValue 类中设置常量
package com.jssso.htgl.util;
public class StaticValue {
public static final String COOKIES_ROLE_KKEY = "COOKIES_ROLE_KKEY";
public static final Long MEMCAED_ROLE_KKEY = 120l; //分钟
}
(二)MyCache 为一个单例模式,这样会更加安全,每次调用的时候会创建一个私有实例
1.add方法为向memcached中添加查到的集合,在方法中
MEMCAED_ROLE_KKEY为设置的分钟数,可以从类StaticValue 中取到设置的值(注意此处设置的时间为120分钟:120l)
2.getCache方法为从memcached中取出缓存的数据
package com.zxrytj.netty; import java.util.Date;
import java.util.List; import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
import com.jssso.htgl.util.StaticValue; public class MyCache {
private static MemCachedClient client=new MemCachedClient();
private static MyCache myCache=new MyCache();
private MyCache(){};
public static MyCache getinstance()
{
return myCache;
}
static
{
String [] addr ={"127.0.0.1:11211"};
Integer [] weights = {3};
SockIOPool pool = SockIOPool.getInstance();
pool.setServers(addr);
pool.setWeights(weights);
pool.setInitConn(5);
pool.setMinConn(5);
pool.setMaxConn(200);
pool.setMaxIdle(1000*30*30);
pool.setMaintSleep(30);
pool.setNagle(false);
pool.setSocketTO(30);
pool.setSocketConnectTO(0);
pool.initialize();
}
public boolean add(String key,List list)//add
{
/**
* date为memcached过期时间
* MEMCAED_ROLE_KKEY,分钟
*/
Date date = new Date(StaticValue.MEMCAED_ROLE_KKEY * 60 * 1000);//2小时,单位毫秒
// System.out.println( "XXXXXXXX"+(List)client.get(key));
return client.add(key, list,date);
}
public List getCache(String key)
{
List list=null;
boolean b=client.keyExists(key);
if(b==true)
{
list=(List) client.get(key);
//System.out.println( "XXXXXXXXlist.size()"+list.size());
}
return list;
} }
二,在Servlet中的调用add,getCache方法
通过判断cookie是否过期向memcached中取数据,为说明问题把关键部分摘出来加以说明
1.cookie存在就直接向memcached中取数据
2.cookie不存在就调用add方法向memcached中添加数据集合
Cookie[] cookies = request.getCookies();//这样便可以获取一个cookie数组
String ckvalue="";//声明一个cookie的value,判断cookie是否存在
List listcahe=null;//这里声明一个集合存放数据
//该for循环判断cookie是否存在
for(Cookie cookie : cookies)
{
if(cookie.getName().equalsIgnoreCase(StaticValue.COOKIES_ROLE_KKEY))//cookie的key值存在,ckvalue存在,直接从memcache中取值
{
ckvalue=cookie.getValue();
System.out.println("ckvalue存在"+ckvalue);
MyCache cache=MyCache.getinstance();
listcahe=cache.getCache(ckvalue);//从memcached中取值赋值给listcache } }
//cookie不存在
if("".equals(ckvalue))//ckvalue不存在
{
System.out.println("ckvalue不存在"+ckvalue);
MyCache cache=MyCache.getinstance();
String uuid = UUID.randomUUID().toString();
/**
* COOKIES_ROLE_KKEY为cookie的key
*/
Cookie ck=new Cookie(StaticValue.COOKIES_ROLE_KKEY,uuid);
ck.setMaxAge(-1);//过期时间,单位为秒,-1关闭浏览器cookie消失
ck.setPath("/js_sso_server/");
response.addCookie(ck);
listcahe = roleService.getApp(rname, rdes, pageIndex, pageSize);//cookie不存在,从数据库获取数据
cache.add(uuid,listcahe);
}
下面是Servlet中的完整代码,可以不看(自己可以练手的可以复制试试)
package com.jssso.htgl.action; import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID; import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONArray;
import net.sf.json.JSONObject; import com.jssso.htgl.entity.Role;
import com.jssso.htgl.service.RoleService;
import com.jssso.htgl.service.impl.RoleServiceImpl;
import com.jssso.htgl.util.GetChche;
import com.jssso.htgl.util.StaticValue;
import com.zxrytj.netty.MyCache; @SuppressWarnings("serial")
public class RoleServlet extends HttpServlet { @Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}; @Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String option = request.getParameter("option");
if (option != null) {
if ("selectApp".equals(option)) {
selectApp(request, response);
} else if ("saveApp".equals(option)) {
saveApp(request, response);
} else if ("updateApp".equals(option)) {
updateApp(request, response);
} else if ("deleteApp".equals(option)) {
deleteApp(request, response);
} else if ("index".equals(option)) {
goView("/WEB-INF/view/jsp/htgl/role.jsp", request, response);
return;
} else if("jsonRole".equals(option)){
jsonRole(request, response);
}
}
} /**
* 删除应用地址
*
* @param request
* @param response
* @throws IOException
*/
private void deleteApp(HttpServletRequest request,
HttpServletResponse response) throws IOException {
String urlId = request.getParameter("rid");
RoleService roleService = new RoleServiceImpl();
boolean flag = roleService.deleteApp(urlId);
PrintWriter out = response.getWriter();
if (flag) {
out.print("success");
} else {
out.print("error");
}
out.flush();
out.close();
} /**
* 修改应用地址
*
* @param request
* @param response
* @throws IOException
*/
private void updateApp(HttpServletRequest request,
HttpServletResponse response) throws IOException {
String urlId = request.getParameter("rid");
String name = request.getParameter("rname");
String url = request.getParameter("des");
Long rid = null;
if (urlId != null) {
rid = Long.parseLong(urlId);
}
Role role = new Role(rid, name, url);
RoleService roleService = new RoleServiceImpl();
boolean flag = roleService.updateApp(role);
PrintWriter out = response.getWriter();
if (flag)
out.print("success");
else
out.print("error");
out.flush();
out.close();
} /**
* 添加应用地址
*
* @param request
* @param response
* @throws IOException
*/
private void saveApp(HttpServletRequest request,
HttpServletResponse response) throws IOException {
String rname = request.getParameter("rname");
String rdes = request.getParameter("des");
RoleService roleService = new RoleServiceImpl();
boolean flag = roleService.saveApp(rname, rdes);
PrintWriter out = response.getWriter();
if (flag)
out.print("success");
else
out.print("error");
out.flush();
out.close();
} /**
* 查询应用地址
*
* @param request
* @param response
* @throws IOException
*/
private void selectApp(HttpServletRequest request,
HttpServletResponse response) throws IOException {
String rname = request.getParameter("rname");
String rdes = request.getParameter("des"); if(rname!=null && !"".equals(rname.trim())){
try { rname = URLDecoder.decode(rname, "utf-8"); } catch (Exception e) { e.printStackTrace(); }
} Long pageIndex = 1L;
if (request.getParameter("page") != null) {
pageIndex = Long.parseLong(request.getParameter("page"));
if (pageIndex == null || pageIndex == 0) {
pageIndex = 1L;
}
}
Integer pageSize = 10;
if (request.getParameter("rows") != null) {
pageSize = Integer.parseInt(request.getParameter("rows"));
if (pageSize == null) {
pageSize = 10;
}
}
RoleService roleService = new RoleServiceImpl(); Cookie[] cookies = request.getCookies();//这样便可以获取一个cookie数组
String ckvalue="";
List listcahe=null;
for(Cookie cookie : cookies)
{
if(cookie.getName().equalsIgnoreCase(StaticValue.COOKIES_ROLE_KKEY))//cookie的key值存在,ckvalue存在,直接取值listcacahe
{
ckvalue=cookie.getValue();
System.out.println("ckvalue存在"+ckvalue);
MyCache cache=MyCache.getinstance();
listcahe=cache.getCache(ckvalue); } }
if("".equals(ckvalue))//ckvalue不存在
{
System.out.println("ckvalue不存在"+ckvalue);
MyCache cache=MyCache.getinstance();
String uuid = UUID.randomUUID().toString();
/**
* COOKIES_ROLE_KKEY为cookie的key
*/
Cookie ck=new Cookie(StaticValue.COOKIES_ROLE_KKEY,uuid);
ck.setMaxAge(-1);//过期时间,单位为秒,-1关闭浏览器cookie消失
/**
* 获取web-inf路径
*/
//String path=request.getSession().getServletContext().getRealPath("/WEB-INF/cookie");
// System.out.println("&&&&&&&&&&&&&&&&&path:"+path);
ck.setPath("/js_sso_server/");
response.addCookie(ck);
listcahe = roleService.getApp(rname, rdes, pageIndex, pageSize);
cache.add(uuid,listcahe);
}
//GetChche get=new GetChche();
// get.getCache(request);
Long count = roleService.pageCount(rname, rdes);
Long pageCount = count % pageSize == 0 ? count / pageSize
: (count / pageSize) + 1;
Map<String, Object> jsonMap = new HashMap<String, Object>();// 定义map
jsonMap.put("total", count);
jsonMap.put("rows", listcahe);
jsonMap.put("pages", pageCount);
jsonMap.put("from", (pageIndex - 1) * pageSize + 1);
jsonMap.put("to", pageIndex * pageSize);
PrintWriter out = response.getWriter();
out.print(JSONObject.fromObject(jsonMap).toString());
out.flush();
out.close();
}
/**
* 查询应用地址
*
* @param request
* @param response
* @throws IOException
*/
private void jsonRole(HttpServletRequest request,
HttpServletResponse response) throws IOException { Long pageIndex = 1L;
if (request.getParameter("page") != null) {
pageIndex = Long.parseLong(request.getParameter("page"));
if (pageIndex == null || pageIndex == 0) {
pageIndex = 1L;
}
}
Integer pageSize = 10;
if (request.getParameter("rows") != null) {
pageSize = Integer.parseInt(request.getParameter("rows"));
if (pageSize == null) {
pageSize = 10;
}
}
RoleService roleService = new RoleServiceImpl();
List<Role> json = roleService.getApp(null,null, pageIndex, pageSize); PrintWriter out = response.getWriter(); out.print(JSONArray.fromObject(json).toString());
out.flush();
out.close();
} private void goView(String url, HttpServletRequest request,
HttpServletResponse response) {
RequestDispatcher dispatcher = request.getRequestDispatcher(url);
try {
dispatcher.forward(request, response);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
通过判断cookie过期方式向Memcached中添加,取出数据(Java)的更多相关文章
- 向SDE图层中添加大量数据时,出现ORA-00604以及ORA-01000的解决办法
转自原文 向SDE图层中添加大量数据时,出现ORA-00604以及ORA-01000的解决办法 写了一个小程序,从一个列表中读取坐标串,每个坐标串生成一个IPolygon,然后将这些Polygon添加 ...
- C#中向ListView控件中添加一行数据
C#中向ListView控件中添加一行数据: ,先声明一个ListViewItem: ListViewItem item = new ListViewItem(); ,添加第一列数据: item.Te ...
- .NetCore 中使用AppMetrics向InfluxDB中添加监控数据并通过Grafana图像分析
考虑到分布式部署监控环境是所有的请求情况,所以这一块一般在网关GateWay里面加比较省事,聚合在一起的,如果放在api服务中,如果只有1个还好,一旦部署Node多是很痛苦的事情 这天需要添加的Nug ...
- 以 Composer 的方式在 PhpStorm 中添加代码审查工具 Code Sniffer
一.前提条件 Windows 操作系统 可以在本地运行的 PHP 环境,个人用的是 laragon PhpStorm Composer 二.设置步骤 (一)下载 Code Sniffer 主要使用 P ...
- 让input支持 ctrl v上传粘贴图片? 让input支持QQ截图或剪切板中的图像数据(Java实现保存)
原理:监听粘贴 → 获取粘贴内容 → 将内容上传 → 抓取后返回替换至input 我们在生产中用到的界面: 测试地址 http://sms.reyo.cn 用户名:aa 密码:123456 以下是PH ...
- 从文件中读取数组数据————Java
自己总结一下Java文件的读取类似数组数据的方法,自己可以快速查看. 一.规整化数据: 对于数组数据是一一对应的情况 ArrayList<String> arrayList = new A ...
- 原生dapper中新增用户后根据用户id,在用户角色表中添加关联数据,事务处理
var result = 0; var userId = 0; using (var db = _Sql.Connection) using (var tran =db.BeginTransactio ...
- 给DataTable中添加一行数据
一.如果该DataTable有两列,列的名称是Name,Age,且该DataTable的名称是dt; DataRow dr = dt.NewRow(); dr["Name"] = ...
- 向listview控件中添加数据库数据
//连接字符串 string str = "Data Source=.;Initial Catalog=mu;User ID=sa;Password=111"; //创建数据库连接 ...
随机推荐
- 转 [ javascript面向对象技术
以下文章来自iteye,作者是 sdcyst ,个人主页 http://www.iteye.com/topic/288813 类变量/类方法/实例变量/实例方法先补充一下以前写过的方法:在javasc ...
- Dom编程(三)
简单菜单制作,参考代码: var ul = document.getElementById("parent"); var lis = ul.getElementsByTagName ...
- php+socket模拟表单发送请求
<?php /** * http请求类(php + socket) * @todo 这里还有很多未完善的地方,仅有简单的get post head请求 * @author chuangrain@ ...
- iOS制作毛玻璃效果
//添加一个图片 UIImageView *imageview = [[UIImageView alloc]init]; imageview.frame = CGRectMake(10, 100, 3 ...
- 《C程序设计语言》读书笔记----习题1-20
练习1-20:编写程序detab,将输入中的制表符替换成适当数目的空格,使得空格充满到下一个制表符终止位的地方,.假设制表符终止位的位置时固定的,比如每隔n列就会出现一个终止位. 这里要理解“制表符” ...
- UVa 900 - Brick Wall Patterns
题目大意:用1*2的砖头建n*2的墙,问有多少种不同的砖头排列方式?与斐波那契序列相似. #include <cstdio> #define MAXN 60 #define N 50 un ...
- BZOJ2318: Spoj4060 game with probability Problem
#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #i ...
- 10-instanceof
在Java中可以使用instanceof关键字判断一个对象到底是不是一个类的实例 package com.example; class A{ void tell1(){ System.out.prin ...
- Grunt构建工具插件篇——之less工具3和watch配合自动化编译
grunt less转换成css速度慢 而且页面会全部刷新? 最近遇到了个问题,grunt里用less,当修改完.less里面的样式,对应 的.css文件会好几秒才修改,然后浏览器上显示也会耗时好几秒 ...
- jquery proxy
slice = Array.prototype.slice,// Bind a function to a context, optionally partially applying any // ...