资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

RedisUtil工具类

package com.amway.msgcenter.msgtask.util;

10余年的柳江网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。营销型网站的优势是能够根据用户设备显示端的尺寸不同,自动调整柳江建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联从事“柳江网站设计”,“柳江网站推广”以来,每个客户项目都认真落实执行。

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);

        }
    });

}

}


文章题目:RedisUtil工具类
转载来于:http://cdkjz.cn/article/pdehcs.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220