Before has written the data dictionary related business is how to do, has the friend message asks how to read, actually very simple, the simplest way is every time reads the database, but this kind of practice is very bad, because the data dictionary belongs to the cold resource in the database, does not often become the data, such data uses the cache to do is best, Access to the database before the request to the database is directly intercepted, so as to reduce the pressure on the database, after all, many pages for the data dictionary access is still too much
Just take a quick look at a picture:
So how do you put the data into Redis? The mainstream of the two ways to say it roughly:
After each operation of the edit data dictionary, a message queue is sent, which is recorded separately in the Redis maintenance (this way I personally do not like it, because sometimes it is wrong to rewrite or delete, so frequent operation more)
After each data dictionary additions and deletions, and finally manually perform the refresh, so that the bulk of the data into the cache, and finally on the page can be directly used
In this small part I use the second way,
On the previous page with a refresh cache button, dynamic Ajax to the background, read the database data dictionary and then flush into the cache (some people say that such frequent operation will be very bad, that is to say, but the operation of this permission only after the successful release of the project, and then perform the operation, is generally midnight, no impact. --)
Let's take a look at the database data
And look at the data in the cache.
It's not hard to see that all one by one correspond
Finally, to look at the custom label, the main purpose is to accept the JSP on the key and the corresponding data dictionary code, and finally return the value
PrivateJedisclient Jedis = Springutils.getcontext (). Getbean (jedisclient.class); Public voidDotag ()throwsjspexception, IOException {if(Stringutils.isnotempty (TypeCode) &&Stringutils.isnotempty (Ddkey)) {String Ddvalue= Jedis.get (cache_data_dicts + ":" + TypeCode + ":" +Ddkey); JspWriter out=Getjspcontext (). Getout (); Out.println (Ddvalue); } Else{getjspbody (). Invoke (SW); Getjspcontext (). Getout (). println (Sw.tostring ()); } }
<Divclass= "caption">Gender:<Datadict:datadictvalueDdkey= "1"TypeCode= "Sex"/> <BR/>type of vehicle:<Datadict:datadictvalueDdkey= "1"TypeCode= "Car_type"/> </Div>
Page effect:
Off-topic, this approach is limited to JSP pages, if your data is from JS render out words (such as jqgrid ah, etc.), then you can write a JS class object, each time incoming code and key call Ajax read from the cache.
?
Several ways to refresh and read the data dictionary cache