Redis application Summary

Source: Internet
Author: User
Tags delete cache

1. Install

$ wget http://redis.googlecode.com/files/redis-2.6.14.tar.gz$ tar xzf redis-2.6.14.tar.gz$ cd redis-2.6.14$ make $ make install

Start the service:
/Usr/local/bin/redis-Server

Client command:
/Usr/local/bin/redis-cli

2. Configuration

In the source file redis-2.6.14/utils There Is A redis_init_script can be used as the redis Startup Script File

Redisport = 6379 # port number
Exec =/usr/local/bin/redis-Server
Cliexec =/usr/local/bin/redis-cli

Pidfile =/var/run/redis _ $ {redisport}. PID
Conf = "/usr/local/redis. conf" # configuration file address

Redisport = 6379 and conf can be modified.

There is a configuration file for redis. conf in the redis-2.6.14, which can be mainly configured

Port 6379 # port number

Daemonize yes # Run in the background

DIR/mnt/redisdata # configure where persistent files are stored

Maxclients 5000 # maximum number of client connections

Maxmemory 1500000000 # maximum memory usage <1.5G>

Requirepass mypass # Access Password

Then place the execution script and configuration file in a directory, as shown in figure

/Usr/local/redis/bin/redis_init_script
/Usr/local/redis. conf

3. Run and stop

Run/usr/local/redis/bin/redis_init_script start/usr/local/redis. conf

Stop/usr/local/redis/bin/redis_init_script stop/usr/local/redis. conf

4. Integration of spring and redis

<! -- Jedis pool configuration --> <bean id = "jedispoolconfig" class = "redis. clients. jedis. jedispoolconfig "> <property name =" maxactive "value =" 1024 "/> <property name =" maxidle "value =" 200 "/> <property name =" maxwait "value = "1000"/> <property name = "testonborrow" value = "true"/> </bean> <! -- Spring data redis --> <bean id = "jedisconnectionfactory" class = "org. springframework. data. redis. connection. jedis. jedisconnectionfactory "> <property name =" usepool "value =" true "/> <property name =" hostname "value =" 192.168.0.111 "/> <property name =" Port "value = "6379"/> <property name = "password" value = ""/> <property name = "timeout" value = "100000"/> <constructor-Arg Index = "0 "ref =" jedispoolconfig "/> </bean> <bean id =" redistemplate "class =" org. springframework. data. redis. core. redistemplate "> <property name =" connectionfactory "ref =" jedisconnectionfactory "/> </bean> <bean id =" redisbase "abstract =" true "class =" com. itmg. cache. impl. redisbase "> <property name =" redistemplatebase "ref =" redistemplate "/> </bean> <bean id =" rediscachebase "class =" com. itmg. cache. impl. rediscachebase "> <property name =" template "ref =" redistemplate "/> </bean> <bean id =" searchcacheservice "class =" com. itmg. cache. impl. searchcacheserviceimpl "parent =" rediscachebase "/> <bean id =" updatecacheservice "class =" com. itmg. cache. impl. updatecacheserviceimpl "parent =" rediscachebase "/>

Package COM. cache. impl; import Java. util. arraylist; import Java. util. list; import Org. springframework. dao. dataaccessexception; import Org. springframework. data. redis. connection. redisconnection; import Org. springframework. data. redis. core. rediscallback; import Org. springframework. data. redis. core. redistemplate; import Org. springframework. data. redis. serializer. redisserializer; import COM. model. keywordsmodel; imp Ort com. VO. awsdatavo;/***** @ author administrator **/public class rediscachebase {private redistemplate template; Public rediscachebase () {// todo auto-generated constructor stub}/*** read cache * @ Param key * @ return */public object readcache (final string key) {object result = (objectmediatemplate.exe cute (New rediscallback <Object> () {public object doinredis (redisconnection connection) throws dataaccessex Ception {redisserializer <string> serializer = template. getdefaseriserializer (); byte [] keybyte = serializer. serialize (key); byte [] value = connection. get (keybyte); If (value = NULL) {return NULL;} object u = (object) template. getdefaseriserializer (). deserialize (value); Return U ;}); return result ;} /*** read cache ** @ Param key * @ return */public list <Object> readlistcache (final string key) {list <objec T> objlist = (list <Object> Custom template.exe cute (New rediscallback <Object> () {public list <Object> doinredis (redisconnection connection) throws dataaccessexception {redisserializer <string> serializer = template. getdefaseriserializer (); byte [] keybyte = serializer. serialize (key); long length = connection. llen (keybyte); List <byte []> values = connection. lrange (keybyte, 0, length); If (values = NULL) {re Turn NULL;} List <Object> objlist = new arraylist <Object> (); For (byte [] value: values) {object u = (object) template. getdefaseriserializer (). deserialize (value); objlist. add (U);} connection. del (keybyte); Return objlist ;} /*** add a single element to the list * @ Param key * @ Param value * @ Param livetime * @ return */Public Boolean addaobjecttolist (final string key, final object value, final integer Li Vetime) {boolean result = (boolean)template.exe cute (New rediscallback <Boolean> () {public Boolean doinredis (redisconnection connection) throws dataaccessexception {redisserializer serializer = template. getdefaseriserializer (); byte [] keys = serializer. serialize (key); byte [] objval = serializer. serialize (value); connection. lpush (keys, objval);/* If (null! = Livetime & livetime> 0) {connection. expire (keys, livetime) ;}*/return true ;}}); return result ;} /*** create or update the cache * @ Param key * @ Param value * @ Param livetime * @ return */Public Boolean savecache (final string key, final object value, final integer livetime) {boolean result = (boolean=template.exe cute (New rediscallback <Boolean> () {public Boolean doinredis (redisconnection connection) throws dataacc Essexception {redisserializer serializer = template. getdefaseriserializer (); byte [] keys = serializer. serialize (key); byte [] objval = serializer. serialize (value); connection. set (keys, objval); If (null! = Livetime & livetime> 0) {connection. expire (keys, livetime);} return true ;}}); return result ;}/ *** Delete cache, single * @ Param key * @ return */Public Boolean removecache (final string key) {boolean result = (boolean=template.exe cute (New rediscallback <Boolean> () {public Boolean doinredis (redisconnection connection) throws dataaccessexception {redisserializer serializer = template. getdefaseriserializer (); byte [] keys = serializer. serialize (key); connection. del (KEYS); Return true ;}}); return result ;}public redistemplate gettemplate () {return template;} public void settemplate (redistemplate template) {This. template = template ;}}

package com.cache.impl;import java.util.List;import com.cache.SearchCacheService;import com.model.KeywordsModel;import com.util.Constants;import com.vo.AwsDataVO;import com.vo.SearchResultVO;public class SearchCacheServiceImpl extends RedisCacheBase implements SearchCacheService {public List<KeywordsModel> getIndexPageWordList() throws Exception {Object obj = readCache(Constants.CACHE_NAME_INDEX_PAGE);if(obj != null){List<KeywordsModel> indexPageList = (List<KeywordsModel>)obj;return indexPageList;}return null;}public SearchResultVO getSearchResultVO(String conditionMD5)throws Exception {Object obj = readCache(Constants.CACHE_NAME_SEARCH_RESULT+conditionMD5);if(obj != null){SearchResultVO searchResultVO = (SearchResultVO)obj;return searchResultVO;}return null;}public List<AwsDataVO> getAmazonIndexData() throws Exception {Object obj = readListCache(Constants.CACHE_NAME_SAVE_CREATE_INDEX);if(obj != null){return (List<AwsDataVO>)obj;}return null;}public List<KeywordsModel> getKeywordsList() throws Exception {Object obj = readListCache(Constants.CACHE_NAME_SAVE_FILTER_KEYWORDS);if (obj != null) {return (List<KeywordsModel>)obj;}return null;}}

package com.cache.impl;import java.util.List;import com.cache.UpdateCacheService;import com.model.KeywordsModel;import com.util.Constants;import com.vo.AwsDataVO;import com.vo.SearchResultVO;public class UpdateCacheServiceImpl extends RedisCacheBase implements UpdateCacheService {private Object objectkeywords = new Object();private Object objectindex = new Object();public void saveIndexPageWordList(List<KeywordsModel> keywordsModellist)throws Exception {saveCache(Constants.CACHE_NAME_INDEX_PAGE, keywordsModellist, 3600*24);}public void saveSearchResultVO(String conditionMD5,SearchResultVO searchResultVO) throws Exception {saveCache(Constants.CACHE_NAME_SEARCH_RESULT+conditionMD5, searchResultVO, 3600*24*5);}public void saveAmazonIndexData(List<AwsDataVO> awsDataModelList) throws Exception {synchronized (objectindex) {for(AwsDataVO awsDataVO : awsDataModelList){addAObjectToList(Constants.CACHE_NAME_SAVE_CREATE_INDEX, awsDataVO, 3600*24*2);}}}public void saveKeywordsList(List<KeywordsModel> keywordsModelList)throws Exception {synchronized (objectkeywords) {for(KeywordsModel keywordsModel : keywordsModelList){addAObjectToList(Constants.CACHE_NAME_SAVE_FILTER_KEYWORDS, keywordsModel, 3600*24*2);}}}}
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.