Key Methods for Java to achieve Redis persistence to the database

Source: Internet
Author: User

Some projects use redis storage. It is troublesome to rehash redis. So I wrote a simple key method to read redis to the database. For reference only.

Package com. redis. web; import java. util. date; import java. util. iterator; import java. util. set; import redis. clients. jedis. jedis; import com. redis. entity. redisTable; public class RedisPersistence {/*** Method for storing all redis objects */public static void saveAllRedis (final String redisIp, final int redisPort, final String appCode) {Jedis redis = new Jedis (redisIp, redisPort); // connect to redis // redis. auth ("redis"); // verify the password // KEY operation Set keys = redis. keys ("*"); // list all keys and find specific keys such as redis. keys ("foo") Iterator t1 = keys. iterator (); while (t1.hasNext () {Object obj1 = t1.next (); saveRedisObject (redis, obj1 + "", redisIp, redisPort + "", appCode );}} /*** store a single object * @ param redis * @ param redisKey * @ param macIp * @ param port * @ param appCode */private static void saveRedisObject (final Jedis redis, final String redisKey, final String macIp, final String port, final String appCode) {String redisType = redis. type (redisKey); RedisTable redisTable = new RedisTable (); redisTable. setAppCode (appCode); redisTable. setCreateTime (new Date (); redisTable. setMacIp (macIp); redisTable. setPort (port); redisTable. setRedisKey (redisKey); redisTable. setRedisType (redisType); redisTable. setRemark (""); redisTable. setUpdateTime (new Date (); // set if ("set ". equalsIgnoreCase (redisType) {Set
 
  
SetStrings = redis. smembers (redisKey); // obtain all the set sets of the key if (null! = SetStrings &&! SetStrings. isEmpty () {Iterator setIterator = setStrings. iterator (); while (setIterator. hasNext () {Object obj1 = setIterator. next (); redisTable. setRedisValue (obj1 + ""); printRedis (redisTable); // save each set record} // hash set} else if ("hash ". equalsIgnoreCase (redisType) {Set
  
   
HashSets = redis. hkeys (redisKey); if (null! = HashSets &&! HashSets. isEmpty () {Iterator setIterator = hashSets. iterator (); while (setIterator. hasNext () {String objectName = setIterator. next () + ""; redisTable. setObjectName (objectName); redisTable. setRedisValue (redis. hget (redisKey, objectName); printRedis (redisTable); // save each set record} // list set} else if ("list ". repeated signorecase (redisType) {Long listLen = redis. llen (redisKey); for (Long I = 0L; I <listLen; I ++) {redisTable. setRedisValue (redis. lindex (redisKey, I); printRedis (redisTable);} // sortedset} else if ("sortedset ". repeated signorecase (redisType) {// Long redisLenth = redis. zcard (redisKey); Set
   
    
Sortedsets = redis. zrange (redisKey, 0,-1); if (null! = Sortedsets &&! Sortedsets. isEmpty () {Iterator setIterator = sortedsets. iterator (); while (setIterator. hasNext () {String sortedMember = setIterator. next () + ""; redisTable. setRedisValue (sortedMember); redisTable. setScore ("" + redis. zscore (redisKey, sortedMember); printRedis (redisTable); // save each sortedset record} // string set} else if ("string ". repeated signorecase (redisType) {redisTable. setRedisValue (redis. get (redisKey); printRedis (redisTable); // save record} else {System. out. println ("UnknowRedisType ----- redisType:" + redisType + "objValue:" + redis. get (redisKey) ;}}// print the output public static void printRedis (RedisTable redisTable) {System. out. println ("redisType:" + redisTable. getRedisType () + "redisKey:" + redisTable. getRedisKey () + "ObjectName:" + redisTable. getObjectName () + "redisValue:" + redisTable. getRedisValue () + "redisScore:" + redisTable. getScore ();} public static void main (String [] args) {String redisIp = "127.0.0.1"; // redis IP address int redisPort = 6379; // redis port number String appCode = "FUYOU"; // appcodesaveAllRedis (redisIp, redisPort, appCode) differentiated by different applications );}}
   
  
 


The appCode is not required. Here, we only need to use the same redis for persistent backup for different applications (identified by appcode ).

The RedisTable instance used is as follows:

Package com. redis. entity; import java. util. date; public class RedisTable {private Long redisId; // Save the redis primary key ID private String redisType; // The redis type is as follows: set/list/hash/sortedset/string private String redisKey; // key private String objectName used to save redis; // This attribute is mainly used for hash data structures, save the private String redisValue of member; // The value of the stored redis private String keyToken; // when saving the Token, it is the private String score that distinguishes the concatenated strings; // when this attribute is a sortedset data structure The saved score value is private Date createTime; // creation time private Date updateTime; // Update time private String macIp; // The redis IP Address can also be stored here as private String port; // redis port number private String appCode; // application differentiation code private String remark; // remarks private String isModify; // whether to modify. This attribute can be used for Incremental backup, that is, an attribute isModify can be stored with more keys during each redis storage. If any modification is made, set it to Y. Otherwise, set it to N. /// // The setter and getter methods are omitted here /////////// /////////////}



The above method can be expanded for the redis backup center.

When using redis Incremental backup, check whether the data block exists based on the rediskey. Update or add operations based on the value 0 or 1 returned by the update.

Of course, the table structure of the database is currently only one table. If the data volume is large, you can store the port, IP address, and appCode information to a separate table to reduce data redundancy.

The above time complexity and efficiency are not optimized. If the requirement is relatively high, optimize it on your own.


Hope everyone can give suggestions and make progress together!





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.