Redis SPRINGMVC Configuration and application

Source: Internet
Author: User

1. Create a Constantutil class

/**
* Redis Profile Name
*/
Public final static String redis_file_name_config = "Redis.properties";

2. Add the Pom file

<!--Https://mvnrepository.com/artifact/redis.clients/jedis--
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>

3. Configure a Redis configuration file

#redis服务器IP
redis.addr=10.12.12.140

#redis的端口号
redis.port=6379

#可用连接实例的最大数目, the default is 8 if the assignment is-1, it means no limit, and if the pool is already assigned maxactive Jedis instances, the pool's state is exhausted (exhausted).
redis.maxactive=1024

#控制一个pool最多有多少个状态为idle (Idle) instance of Jedis, which is also 8 by default
redis.maxidle=200

#等待可用连接的最大时间, in milliseconds, the default value is-1, which means that never times out. If the wait time is exceeded, the jedisconnectionexception is thrown directly
redis.maxwait=10000

#初始化连接池超时时间
redis.timeout=10000

4, write Redisutil tool class

Package Com.aspire.prnp.redis;

Import java.util.List;
Import Java.util.Map;

Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;

Import Com.aspire.prnp.util.StringUtil;

Import Redis.clients.jedis.Jedis;
Import Redis.clients.jedis.JedisPool;
Import Redis.clients.jedis.JedisPoolConfig;

public class Redisutil {
private static final Logger Logger = Loggerfactory.getlogger (Redisutil.class);
Access password
private static String AUTH = "root";

Whether the validate operation is performed in advance when a Redis instance is borrow;
If true, the resulting Jedis instances are available
private static Boolean Test_on_borrow = true;

private static Jedispool Jedispool = null;

/**
*
* Method_name:setredisparamters
* date:2017 June 15 5:23:27
* Return_type:void
* Description: Initializing Redis connection Pool
*/
public static void Setredisparamters (String addr,int port,int timeout,int maxactive,int maxidle,int maxwait) {
try {
Jedispoolconfig config = new Jedispoolconfig ();
Config.setmaxtotal (maxactive);//The old version is setmaxactive
Config.setmaxidle (Maxidle);
Config.setmaxwaitmillis (maxwait);//The old version is maxmaxwait
Config.settestonborrow (Test_on_borrow);
Jedispool = new Jedispool (config,addr,port,timeout);//incoming auth with password
} catch (Exception e) {
E.printstacktrace ();
}

}

/**
*
* Method_name:getjedis
* date:2017 June 15 5:41:24
* Return_type:jedis
* Description: Get Jedis instance
*/
Public synchronized static Jedis Getjedis () {
try {
if (Jedispool! = null) {
Jedis resource = Jedispool.getresource ();
return resource;
}else{
return null;
}
} catch (Exception e) {
E.printstacktrace ();
return null;
}
}



/**
*
* Method_name:returnresource
* date:2017 June 15 5:41:36
* Return_type:void
* Description: Release Jedis resources
*/
public static void Returnresource (Jedis Jedis) {
if (Jedis! = null) {
Jedis.close ();
}
}
/**
*
* method_name:setstring
* date:2017 June 22 3:20:01
* Return_type:void
* Description: Set string
*/
public static void SetString (string key, String value) {
try {
Value = Stringutil.isempty (value)? "": value;
Getjedis (). Set (Key,value);
} catch (Exception e) {
Logger.error ("Set key error:" +e);
}
}
/**
*
* method_name:setstring
* date:2017 June 22 3:20:50
* Return_type:void
* Description: Set string and Expiration time
*/
public static void SetString (String key, int seconds,string value) {
try {
Value = Stringutil.isempty (value)? "": value;
Getjedis (). Setex (key, seconds, value);
} catch (Exception e) {
Logger.error ("Set Keyex Error:" +e);
}
}
/**
*
* method_name:getstring
* date:2017 June 22 3:24:32
* return_type:string
* Description:
*/
public static string getString (String key) {
if (getjedis () = = NULL | |!getjedis (). Exists (key)) {
return null;
}
Return Getjedis (). get (key);
}

public static <T> void setlist (String key,list<t> List) {
try {
Getjedis (). Set (Key.getbytes (), objecttranscoder.serialize (list));
} catch (Exception e) {
Logger.error ("Set key error:" +e);
}
}

public static <T> list<t> getList (String key) {
if (getjedis () = = NULL | |!getjedis (). Exists (Key.getbytes ())) {
return null;
}
byte[] in = Getjedis (). Get (Key.getbytes ());
list<t> list = (list<t>) objecttranscoder.deserialize (in);
return list;
}

public static <T> void Setmap (String key,map<string,t> Map) {
try {
Getjedis (). Set (Key.getbytes (), objecttranscoder.serialize (map));
} catch (Exception e) {
Logger.error ("Set key error:" +e);
}
}

public static <T> map<string,t> Getmap (String key) {
if (getjedis () = = NULL | |!getjedis (). Exists (Key.getbytes ())) {
return null;
}
byte[] in = Getjedis (). Get (Key.getbytes ());
map<string,t> map = (map<string, t>) objecttranscoder.deserialize (in);
return map;
}
}

5. Application examples

/**
*
* Method_name:querytrafficcount
* date:2017 June 19 4:46:00
* return_type:map<string,object>
* Description: Query first page business volume and number of users chart
*/
@RequestMapping (value = "/report/querytrafficcount.ajax")
@ResponseBody
Public map<string,object> Querytrafficcount (String type) {
Long startime = System.currenttimemillis ();
try{
Configuration c =configurationhelper.getconfiguration (constantutil.redis_file_name_config);
String addr = c.getstring ("redis.addr");
int port = c.getint ("Redis.port");
int timeOut = C.getint ("Redis.timeout");
int maxactive = C.getint ("redis.maxactive");
int maxidle = C.getint ("Redis.maxidle");
int maxwait = C.getint ("redis.maxwait");
Redisutil.setredisparamters (addr, port, TimeOut, Maxactive, Maxidle, maxwait);
map<string, object> data = null;
if ("D". Equals (type)) {
data = Redisutil.getmap ("Trafficcountforday");
}else if ("M". Equals (type)) {
data = Redisutil.getmap ("Trafficcountformonth");
}else if ("Y". Equals (type)) {
data = Redisutil.getmap ("Trafficcountforyear");
}
if (data = = NULL) {
data = Homepagereportservice.getchart (type);
if ("D". Equals (type)) {
Redisutil.setmap ("Trafficcountforday", data);
}else if ("M". Equals (type)) {
Redisutil.setmap ("Trafficcountformonth", data);
}else if ("Y". Equals (type)) {
Redisutil.setmap ("Trafficcountforyear", data);
}
}

Long endTime = System.currenttimemillis ();
Logger.info ("Query the first page of business volume and number of users controller Time" + (endtime-startime));
return data;
}catch (Exception e) {
Logger.error ("Query first page business volume and number of users", e);
Return Super.fail ("Business volume and number of users");
}
}
/**
*
* Method_name:querytrafficareaformcount
* date:2017 June 22 11:13:19
* return_type:map<string,object>
* Description: Query traffic area data
*/
@RequestMapping (value = "/report/querytrafficareaformcount.ajax")
@ResponseBody
Public map<string,object> Querytrafficareaformcount (String type) {
Long startime = System.currenttimemillis ();
Pagevo pagedata = null;
try{
Configuration c =configurationhelper.getconfiguration (constantutil.redis_file_name_config);
String addr = c.getstring ("redis.addr");
int port = c.getint ("Redis.port");
int timeOut = C.getint ("Redis.timeout");
int maxactive = C.getint ("redis.maxactive");
int maxidle = C.getint ("Redis.maxidle");
int maxwait = C.getint ("redis.maxwait");
Redisutil.setredisparamters (addr, port, TimeOut, Maxactive, Maxidle, maxwait);
map<string, object> data = null;
if ("D". Equals (type)) {
data = Redisutil.getmap ("Trafficareaformcountforday");
}else if ("M". Equals (type)) {
data = Redisutil.getmap ("Trafficareaformcountformonth");
}else if ("Y". Equals (type)) {
data = Redisutil.getmap ("Trafficareaformcountforyear");
}

if (data = = NULL) {
data = Homepagereportservice.getareaformchart (type);
if ("D". Equals (type)) {
Redisutil.setmap ("Trafficareaformcountforday", data);
}else if ("M". Equals (type)) {
Redisutil.setmap ("Trafficareaformcountformonth", data);
}else if ("Y". Equals (type)) {
Redisutil.setmap ("Trafficareaformcountforyear", data);
}
}
Pagedata = new Pagevo (1, 0). Format ();
Pagedata.setlist ((list<map<string,object>>) data.get ("Deliveryareadata"));
Pagedata.settotal (0);
Long endTime = System.currenttimemillis ();
Logger.info ("Query the first page of business volume and number of users controller Time" + (endtime-startime));
return Pagedata.pagemodel ();
}catch (Exception e) {
Logger.error ("Query first page business volume and number of users", e);
Return Super.fail ("Business volume and number of users");
}
}
/**
*
* Method_name:querytrafficareaformcount
* date:2017 June 22 11:13:19
* return_type:map<string,object>
* Description: Query business Volume enterprise data
*/
@RequestMapping (value = "/report/querytrafficexpformcount.ajax")
@ResponseBody
Public map<string,object> Querytrafficexpformcount (String type) {
Long startime = System.currenttimemillis ();
Pagevo pagedata = null;
try{
Configuration c =configurationhelper.getconfiguration (constantutil.redis_file_name_config);
String addr = c.getstring ("redis.addr");
int port = c.getint ("Redis.port");
int timeOut = C.getint ("Redis.timeout");
int maxactive = C.getint ("redis.maxactive");
int maxidle = C.getint ("Redis.maxidle");
int maxwait = C.getint ("redis.maxwait");
Redisutil.setredisparamters (addr, port, TimeOut, Maxactive, Maxidle, maxwait);
map<string, object> data = null;
if ("D". Equals (type)) {
data = Redisutil.getmap ("Trafficexpformcountforday");
}else if ("M". Equals (type)) {
data = Redisutil.getmap ("Trafficexpformcountformonth");
}else if ("Y". Equals (type)) {
data = Redisutil.getmap ("Trafficexpformcountforyear");
}
if (data = = NULL) {
data = Homepagereportservice.getexpformchart (type);
if ("D". Equals (type)) {
Redisutil.setmap ("Trafficexpformcountforday", data);
}else if ("M". Equals (type)) {
Redisutil.setmap ("Trafficexpformcountformonth", data);
}else if ("Y". Equals (type)) {
Redisutil.setmap ("Trafficexpformcountforyear", data);
}
}
Pagedata = new Pagevo (1, 0). Format ();
Pagedata.setlist ((list<map<string,object>>) data.get ("Deliveryexpdata"));
Pagedata.settotal (0);
Long endTime = System.currenttimemillis ();
Logger.info ("Query the first page of business volume and number of users controller Time" + (endtime-startime));
return Pagedata.pagemodel ();
}catch (Exception e) {
Logger.error ("Query first page business volume and number of users", e);
Return Super.fail ("Business volume and number of users");
}
}

PS Service business Layer code omitted

Redis SPRINGMVC Configuration and application

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.