Simple use of query Redis caching

Source: Internet
Author: User

1, interface

/**
* Priority to query member information from the cache
* @param custnum
* @return
*/
Public member Findmemberbycustnumfristbyredis (String custnum);


2. Implementation class

@Override
Public member Findmemberbycustnumfristbyredis (String custnum) {
if (Stringutil.isempty (Custnum)) {//returns null directly when passed in as NULL, the member number must exist
return null;
}
member member = (member) shardedjedisutil.getobj (Redisconst.query_member_by_custnum + custnum);
if (member = = null) {
map<string, object> parameter = new hashmap<string, object> ();
Parameter.put ("Custnum", custnum);
List<member> Members = this.findlist (parameter, NULL);
if (null!= members &&!members.isempty ()) {
Shardedjedisutil.setobjex (Redisconst.query_member_by_custnum + custnum, redisconst.one_hour_seconds, Members.get (0 ));
Return Members.get (0);
}
}
return member;
}



Tool class for 3.redis operations

public class Shardedjedisutil {


Final static Logger Logger = Loggerfactory.getlogger (Shardedjedisutil.class);
protected static Shardedjedisclientimpl shardedjedisclient = null;
static {
Load redis.conf configuration information for the SCM platform to create client objects
Logger.info ("----------------load the redis.conf configuration information of the SCM platform, create Shardedjedisclientimpl client Object--------------");
try {
Shardedjedisclient = new Shardedjedisclientimpl ("redis.conf");
Logger.info ("----------------Create Shardedjedisclientimpl Client object End--------------");
catch (Exception e) {
Logger.error ("--------Create Shardedjedisclient exception, verify that the configuration is correct!--------------------", e);
Shardedjedisclient = null;
}
}



/**
* Set the corresponding object of the string type to the Redis, the user saves the object
*
* @param key
* @param seconds seconds; time cached in Redis
* @param value
* @return
*/
public static object Setobjex (final String key, final int seconds, final Object value) {
if (null = = Shardedjedisclient) {
return null;
}
Return Shardedjedisclient.execute (New shardedjedisaction<object> () {

@Override
Public String doaction (Shardedjedis shardedjedis) {
byte[] keybytes = Objectcopyutil.objecttobyte (key);
byte[] Valuebytes = objectcopyutil.objecttobyte (value);
Return Shardedjedis.setex (keybytes, seconds, valuebytes);
}
});
}


/**
* Get the objects saved in Redis according to key
*
* @param key
* @return
*/
public static Object Getobj (final String key) {
if (null = = Shardedjedisclient) {
return null;
}
Return Shardedjedisclient.execute (New shardedjedisaction<object> () {

@Override
Public Object doaction (Shardedjedis shardedjedis) {
byte[] keybytes = Objectcopyutil.objecttobyte (key);
byte[] valuebytes = Shardedjedis.get (keybytes);
Return Objectcopyutil.bytetoobject (valuebytes);
}
});
}





4. Related constant classes

public class Redisconst {


/** the number of seconds in a minute *
public static final int one_minute_seconds = 60;

/** the number of seconds in an hour * *
public static final int one_hour_seconds = 60 * 60;

/** four hours per second
public static final int four_hour_seconds = 60 * 60 * 4;

/** the number of seconds per day * *
public static final int one_day_seconds = 60 * 60 * 24;

/** One-month number of seconds * *
public static final int one_month_seconds = 60 * 60 * 24 * 30;


/*** through Member Code inquiry member Information * *
public static final String query_member_by_custnum = "Querymemberbycustnum";



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.