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";
}