成都网站建设设计

将想法与焦点和您一起共享

RedisUtil工具类-创新互联

package com.amway.msgcenter.msgtask.util;

创新互联公司成立十多年来,这条路我们正越走越好,积累了技术与客户资源,形成了良好的口碑。为客户提供网站制作、成都网站建设、网站策划、网页设计、申请域名、网络营销、VI设计、网站改版、漏洞修补等服务。网站是否美观、功能强大、用户体验好、性价比高、打开快等等,这些对于网站建设都非常重要,创新互联公司通过对建站技术性的掌握、对创意设计的研究为客户提供一站式互联网解决方案,携手广大客户,共同发展进步。

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 {

@Autowired
// 操作字符串的template,StringRedisTemplate是RedisTemplate的一个子集
private StringRedisTemplate stringRedisTemplate;
@Autowired
// RedisTemplate,可以进行所有的操作
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);

        }
    });

}

}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前名称:RedisUtil工具类-创新互联
文章来源:http://chengdu.cdxwcx.cn/article/gcogo.html