Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

RedisUtil utility class

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Package com.amway.msgcenter.msgtask.util

Import org.springframework.beans.factory.annotation.Autowired

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.core.StringRedisTemplate

Import org.springframework.stereotype.Component

Import redis.clients.jedis.JedisCommands

@ Component

Public class RedisUtil {

The template,StringRedisTemplate of @ Autowired// operation string is a subset of RedisTemplate private StringRedisTemplate stringRedisTemplate;@Autowired// RedisTemplate, and can perform all operations private RedisTemplate redisTemplate;public void set (String key, String value) {stringRedisTemplate.opsForValue (). Set (key, value);} public void set (String key, String value, long time) {stringRedisTemplate.opsForValue (). Set (key, value, time);} public String get (String key) {return stringRedisTemplate.opsForValue () .get (key) } public void delete (String key) {stringRedisTemplate.delete (key);} public Long getLock (String lockKey, String value, int time) {return redisTemplate.execute (new RedisCallback () {@ Override public Long doInRedis (RedisConnection connection) throws DataAccessException {JedisCommands commands = (JedisCommands) connection.getNativeConnection (); Long result = commands.setnx (lockKey, value); commands.expire (lockKey, time); return result }});} public String getLock2 (String lockKey, String value, int time) {return redisTemplate.execute (new RedisCallback () {@ Override public String doInRedis (RedisConnection connection) throws DataAccessException {JedisCommands commands = (JedisCommands) connection.getNativeConnection (); return commands.set (lockKey, value, "NX", "PX", time * 1000L);}});})

}

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report