In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Today's editor to share with you is about Spring-data-redis integration redis method introduction, many people do not understand, today editor in order to let you know more about Spring-data-redis integration redis method, so give you a summary of the following content, let's look down. I'm sure you'll get something.
Spring-data-redis is a part of the spring family, which provides access to redis services through simple configuration in srping applications, and highly encapsulates the underlying reids development packages (Jedis, JRedis, and RJC).
Install the redis service
1. Download and install the redis service, and the redis service will be started automatically after the installation is completed.
Sudo apt-get install redis-server
2. After the download is completed, we can check the redis service process with the command.
Ps-aux | grep redis
The result is shown in the following figure:
3. You can also check the status of the Redis service
The result of netstat-nlt | grep 637 is as follows: # tcp 0 0 127.0.0.1 LISTEN
4. Check the status of redis service by command
The results of sudo / etc/init.d/redis-server status# are as follows: redis-server is running 2. Configure redis
# Edit redis configuration file sudo vim / etc/redis/redis.conf# set redis access password in vim requirepass 12345 comment bind in vim, set to allow remote access By default, redis only allows native access to # bind 127.0.0." restart redis service sudo / etc/init.d/redis-server restart# client access redis service sudo redis-cli# client login and access redis service sudo redis-cli-a youpassword# remote client login and access redis service sudo redis-cli-a youpassword-h 192.168.1.22 III, write java code
1. Cache API definition
Public interface Cache {/ * add * @ param key * @ param value * / void put (Object key, Object value); / * get the value of key * @ param key * @ return * / Object get (Object key); / * * remove key * @ param key * @ return * / Object remove (Object key);}
2. Implementation of cache interface
Import org.springframework.dao.DataAccessException;import org.springframework.data.redis.connection.RedisConnection;import org.springframework.data.redis.connection.RedisListCommands;import org.springframework.data.redis.connection.RedisStringCommands;import org.springframework.data.redis.connection.RedisZSetCommands;import org.springframework.data.redis.connection.jedis.JedisConverters;import org.springframework.data.redis.core.Cursor;import org.springframework.data.redis.core.RedisCallback;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.ScanOptions Import org.springframework.data.redis.core.types.Expiration;import org.springframework.data.redis.serializer.RedisSerializer;import redis.clients.jedis.Jedis;public class DefaultRedisCache implements Cache {private final static Logger logger = LoggerFactory.getLogger (DefaultRedisCache.class); private RedisTemplate redisTemplate; / / s private final static Long DEFAULT_EXPIRE = 12 * 60 * 60L; public DefaultRedisCache (RedisTemplate redisTemplate) {this.redisTemplate = redisTemplate } @ Override public void put (final Object key, final Object value) {put (key, value, DEFAULT_EXPIRE);} @ Override public Object get (final Object key) {return redisTemplate.execute (new RedisCallback () {@ Override public Object doInRedis (RedisConnection connection) throws DataAccessException {RedisSerializer serializer = getRedisSerializer (); byte [] keyByte = serializer.serialize (key)) If (keyByte = = null) {return null;} byte [] bytes = connection.get (keyByte); if (bytes = = null) {return null;} return serializer.deserialize (bytes);}}) } @ Override public Object remove (final Object key) {return redisTemplate.execute (new RedisCallback () {@ Override public Object doInRedis (RedisConnection connection) throws DataAccessException {RedisSerializer serializer = getRedisSerializer (); if (key = = null) {return null;} byte [] bytes = serializer.serialize (key) Return connection.del (bytes);});}
3. Configure the applicationContext-cache-test.xml file
4. Configure the redis-config.properties file
# jedis pool configuration commons.cache.redis.maxTotal=1000commons.cache.redis.maxIdle=600commons.cache.redis.maxWait=1000commons.cache.redis.testOnBorrow=truecommons.cache.redis.testOnReturn=true# spring data redis configuration commons.cache.redis.host=192.168.1.230commons.cache.redis.port=6379commons.cache.redis.password=123456commons.cache.redis.timeout=1000
5. Test
@ ContextConfiguration (locations = {"classpath:applicationContext-cache-test.xml"}) @ RunWith (SpringJUnit4ClassRunner.class) public class CacheTest extends BaseTest {@ Autowired private Cache cache; @ Test public void redisTest () {/ / add the value of key as username to the redis cache cache.put ("username", "nathan"); / / get the value Object username = cache.get ("username") System.out.println ("cache.get =" + username); / / delete value / / cache.remove ("username"); / / Object delResult = cache.get ("username"); / / System.out.println ("cache.remove =" + delResult);}}
These are the details of the method of integrating redis with Spring-data-redis. Have you gained anything after reading it? If you want to know more about it, welcome to the industry information!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.