Java Code Examples for org.springframework.data.redis.core.ValueOperations#set()

The following examples show how to use org.springframework.data.redis.core.ValueOperations#set() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SmsController.java    From ZTuoExchange_framework with MIT License 6 votes vote down vote up
@RequestMapping(value = "/add/address/code", method = RequestMethod.POST)
public MessageResult addAddressCode(@SessionAttribute(SESSION_MEMBER) AuthMember user) throws Exception {
    Member member = memberService.findOne(user.getId());
    Assert.hasText(member.getMobilePhone(), localeMessageSourceService.getMessage("NOT_BIND_PHONE"));
    MessageResult result;
    String randomCode = String.valueOf(GeneratorUtil.getRandomNumber(100000, 999999));
    if ("86".equals(member.getCountry().getAreaCode())) {
        result = smsProvider.sendVerifyMessage(member.getMobilePhone(), randomCode);
    } else {
        result = smsProvider.sendInternationalMessage(randomCode, member.getCountry().getAreaCode() + member.getMobilePhone());
    }
    if (result.getCode() == 0) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String key = SysConstant.PHONE_ADD_ADDRESS_PREFIX + member.getMobilePhone();
        valueOperations.getOperations().delete(key);
        // 缓存验证码
        valueOperations.set(key, randomCode, 10, TimeUnit.MINUTES);
        return success(localeMessageSourceService.getMessage("SEND_SMS_SUCCESS"));
    } else {
        return error(localeMessageSourceService.getMessage("SEND_SMS_FAILED"));
    }
}
 
Example 2
Source File: SmsProviderController.java    From ZTuoExchange_framework with MIT License 6 votes vote down vote up
private MessageResult sendCode(String phone, String prefix) {
    Assert.notNull(phone, msService.getMessage("NO_CELL_PHONE_NUMBER"));
    MessageResult result;
    String randomCode = String.valueOf(GeneratorUtil.getRandomNumber(100000, 999999));
    try {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String key = prefix + phone;
        long expire = valueOperations.getOperations().getExpire(key, TimeUnit.SECONDS);
        if (expire < 600 && expire > 540) {
            return error(msService.getMessage("SEND_CODE_FAILURE_ONE"));
        }
             result = smsProvider.sendVerifyMessage(phone, randomCode);
        if (result.getCode() == 0) {
            logger.info("短信验证码:{}", randomCode);
            valueOperations.set(key, randomCode, 10, TimeUnit.MINUTES);
            return success(msService.getMessage("SEND_CODE_SUCCESS") + phone);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return error(msService.getMessage("REQUEST_FAILED"));
}
 
Example 3
Source File: SmsController.java    From ZTuoExchange_framework with MIT License 6 votes vote down vote up
/**
 * 重置交易密码验证码
 *
 * @param user
 * @return
 */
@RequestMapping(value = "/transaction/code", method = RequestMethod.POST)
public MessageResult sendResetTransactionCode(@SessionAttribute(SESSION_MEMBER) AuthMember user) throws Exception {
    Member member = memberService.findOne(user.getId());
    Assert.hasText(member.getMobilePhone(), localeMessageSourceService.getMessage("NOT_BIND_PHONE"));
    String randomCode = String.valueOf(GeneratorUtil.getRandomNumber(100000, 999999));
    MessageResult result;
    if ("86".equals(member.getCountry().getAreaCode())) {
        result = smsProvider.sendVerifyMessage(member.getMobilePhone(), randomCode);
    } else {
        result = smsProvider.sendInternationalMessage(randomCode, member.getCountry().getAreaCode() + member.getMobilePhone());
    }
    if (result.getCode() == 0) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String key = SysConstant.PHONE_RESET_TRANS_CODE_PREFIX + member.getMobilePhone();
        valueOperations.getOperations().delete(key);
        // 缓存验证码
        valueOperations.set(key, randomCode, 10, TimeUnit.MINUTES);
        return success(localeMessageSourceService.getMessage("SEND_SMS_SUCCESS"));
    } else {
        return error(localeMessageSourceService.getMessage("SEND_SMS_FAILED"));
    }
}
 
Example 4
Source File: AideController.java    From ZTuoExchange_framework with MIT License 6 votes vote down vote up
/**
 * 获取该分类(二级页面)
 * @param pageNo
 * @param pageSize
 * @param cate
 * @return
 */
@RequestMapping(value = "more/help/page",method = RequestMethod.POST)
public MessageResult sysHelpCate(@RequestParam(value = "pageNo",defaultValue = "1")int pageNo,
                                @RequestParam(value = "pageSize",defaultValue = "10")int pageSize,
                                @RequestParam(value = "cate")SysHelpClassification cate){
    ValueOperations valueOperations = redisTemplate.opsForValue();
    JSONObject result = (JSONObject) valueOperations.get(SysConstant.SYS_HELP_CATE+cate);
    if (result != null){
        return success(result);
    }else {
        JSONObject jsonObject = new JSONObject();
        Page<SysHelp> sysHelpPage = sysHelpService.findByCondition(pageNo,pageSize,cate);
        jsonObject.put("content",sysHelpPage.getContent());
        jsonObject.put("totalPage",sysHelpPage.getTotalPages());
        jsonObject.put("totalElements",sysHelpPage.getTotalElements());
        valueOperations.set(SysConstant.SYS_HELP_CATE+cate,jsonObject,SysConstant.SYS_HELP_CATE_EXPIRE_TIME, TimeUnit.SECONDS);
        return success(jsonObject);

    }

}
 
Example 5
Source File: SmsController.java    From ZTuoExchange_framework with MIT License 6 votes vote down vote up
/**
 * 忘记密码验证码
 */
@RequestMapping(value = "/reset/code", method = RequestMethod.POST)
public MessageResult resetPasswordCode(String account) throws Exception {
    Member member = memberService.findByPhone(account);
    Assert.notNull(member, localeMessageSourceService.getMessage("MEMBER_NOT_EXISTS"));
    MessageResult result;
    String randomCode = String.valueOf(GeneratorUtil.getRandomNumber(100000, 999999));
    if ("86".equals(member.getCountry().getAreaCode())) {
        result = smsProvider.sendVerifyMessage(member.getMobilePhone(), randomCode);
    } else {
        result = smsProvider.sendInternationalMessage(randomCode, member.getCountry().getAreaCode() + member.getMobilePhone());
    }
    if (result.getCode() == 0) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String key = SysConstant.RESET_PASSWORD_CODE_PREFIX + member.getMobilePhone();
        valueOperations.getOperations().delete(key);
        // 缓存验证码
        valueOperations.set(key, randomCode, 10, TimeUnit.MINUTES);
        return success(localeMessageSourceService.getMessage("SEND_SMS_SUCCESS"));
    } else {
        return error(localeMessageSourceService.getMessage("SEND_SMS_FAILED"));
    }
}
 
Example 6
Source File: RedisUtils.java    From springboot-learn with MIT License 5 votes vote down vote up
/**
 * 写入缓存
 *
 * @param key
 * @param value
 * @return
 */
@SuppressWarnings("unchecked")
public boolean set(final String key, Object value, Long expireTime) {
    boolean result = false;
    try {
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        operations.set(key, value);
        redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
        result = true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
 
Example 7
Source File: Pub_RedisUtils.java    From SuperBoot with MIT License 5 votes vote down vote up
/**
 * 设置Token的锁定状态
 *
 * @param token 用户Token
 */
public void tokenLocked(String token, boolean locked) {
    ValueOperations<String, HashMap> operations = redisTemplate.opsForValue();
    boolean exists = redisTemplate.hasKey(token);
    if (exists) {
        HashMap tokenItem = operations.get(token);
        if (locked) {
            tokenItem.put(TOKEN_STATUS, TOKEN_STATUS_LOCKED);
        } else {
            tokenItem.put(TOKEN_STATUS, TOKEN_STATUS_UNLOCKED);
        }
        operations.set(token, tokenItem, TOKEN_EXPIRED_DAY, TimeUnit.DAYS);
    }
}
 
Example 8
Source File: SmsController.java    From ZTuoExchange_framework with MIT License 5 votes vote down vote up
/**
 * 绑定手机号验证码
 *
 * @param phone
 * @return
 * @throws Exception
 */
@RequestMapping(value = "/bind/code", method = RequestMethod.POST)
public MessageResult setBindPhoneCode(String country, String phone, @SessionAttribute(SESSION_MEMBER) AuthMember user) throws Exception {
    Member member = memberService.findOne(user.getId());
    Assert.isNull(member.getMobilePhone(), localeMessageSourceService.getMessage("REPEAT_PHONE_REQUEST"));
    MessageResult result;
    String randomCode = String.valueOf(GeneratorUtil.getRandomNumber(100000, 999999));

    // 修改所在国家
    if (StringUtils.isNotBlank(country)) {
        Country one = countryService.findOne(country);
        if (one != null) {
            member.setCountry(one);
            memberService.saveAndFlush(member);
        }
    }


    if ("86".equals(member.getCountry().getAreaCode())) {
        if (!ValidateUtil.isMobilePhone(phone.trim())) {
            return error(localeMessageSourceService.getMessage("PHONE_EMPTY_OR_INCORRECT"));
        }
        result = smsProvider.sendVerifyMessage(phone, randomCode);
    } else {
        result = smsProvider.sendInternationalMessage(randomCode, member.getCountry().getAreaCode() + phone);
    }
    if (result.getCode() == 0) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String key = SysConstant.PHONE_BIND_CODE_PREFIX + phone;
        valueOperations.getOperations().delete(key);
        // 缓存验证码
        valueOperations.set(key, randomCode, 10, TimeUnit.MINUTES);
        return success(localeMessageSourceService.getMessage("SEND_SMS_SUCCESS"));
    } else {
        return error(localeMessageSourceService.getMessage("SEND_SMS_FAILED"));
    }
}
 
Example 9
Source File: RedisUtil.java    From MI-S with MIT License 5 votes vote down vote up
/**
 * 写入缓存
 *
 * @param key
 * @param value
 * @return
 */
public static boolean set(final String key, Object value, Long expireTime) {
    boolean result = false;
    try {
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        operations.set(key, value);
        redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);

        result = true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
 
Example 10
Source File: BizCommentServiceImpl.java    From OneBlog with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 点踩
 *
 * @param id
 */
@Override
@RedisCache(flush = true)
public void doOppose(Long id) {
    String key = IpUtil.getRealIp(RequestHolder.getRequest()) + "_doOppose_" + id;
    ValueOperations<String, Object> operations = redisTemplate.opsForValue();
    if (redisTemplate.hasKey(key)) {
        throw new ZhydCommentException("一个小时只能踩一次哈~又没什么深仇大恨");
    }
    bizCommentMapper.doOppose(id);
    operations.set(key, id, 1, TimeUnit.HOURS);
}
 
Example 11
Source File: BizCommentServiceImpl.java    From OneBlog with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 点赞
 *
 * @param id
 */
@Override
@RedisCache(flush = true)
public void doSupport(Long id) {
    String key = IpUtil.getRealIp(RequestHolder.getRequest()) + "_doSupport_" + id;
    ValueOperations<String, Object> operations = redisTemplate.opsForValue();
    if (redisTemplate.hasKey(key)) {
        throw new ZhydCommentException("一个小时只能点一次赞哈~");
    }
    bizCommentMapper.doSupport(id);
    operations.set(key, id, 1, TimeUnit.HOURS);
}
 
Example 12
Source File: RedisServiceImpl.java    From DouBiNovel with Apache License 2.0 5 votes vote down vote up
@Override
public boolean set(String key, String value) {
    boolean result = false;
    try {
        ValueOperations<String, String> operations = this.template.opsForValue();
        operations.set(key, value);
        result = true;
    } catch (Exception var5) {
        var5.printStackTrace();
    }
    return result;
}
 
Example 13
Source File: RedisServiceImpl.java    From SpringBoot-Dubbo-Docker-Jenkins with Apache License 2.0 5 votes vote down vote up
/**
 * 写入缓存
 *
 * @param key
 * @param value
 * @return
 */
@Override
public boolean set(final String key, Object value, Long expireTime) {
    boolean result = false;
    try {
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        operations.set(key, value);
        redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
        result = true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
 
Example 14
Source File: RedisUtil.java    From shine-mq with Apache License 2.0 5 votes vote down vote up
/**
 * 写入缓存
 *
 * @param key
 * @param value
 * @return
 */
public boolean set(final String key, Object value, Long expireTime) {
    boolean result = false;
    try {
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        operations.set(key, value);
        if (expireTime > 0) {
            expire(key, expireTime);
        }
        result = true;
    } catch (Exception e) {
        log.error("redis set error :", e);
    }
    return result;
}
 
Example 15
Source File: TestController.java    From spring_boot with Apache License 2.0 5 votes vote down vote up
@RequestMapping(value = "/redisSelectTwo")
public String redisSelectTwo(){
    RedisTemplate template = RedisUtil.getSelect_1();
    ValueOperations ops = template.opsForValue();
    ops.set("test","1");
    return "success";
}
 
Example 16
Source File: TestRedis2.java    From spring-boot-demo with MIT License 5 votes vote down vote up
@Test
public void test() {
    ValueOperations<String, String> ops = this.template.opsForValue();
    String key = "DemoRedisApplication";
    if (!this.template.hasKey(key)) {
        ops.set(key, "easy-web");
    }
    log.info("找到key=" + key + ", 值=" + ops.get(key));
}
 
Example 17
Source File: RedisServiceImpl.java    From DouBiNovel with Apache License 2.0 5 votes vote down vote up
@Override
public boolean set(String key, String value, Long expireTime, TimeUnit timeUnit) {
    boolean result = false;
    try {
        ValueOperations<String, String> operations = this.template.opsForValue();
        operations.set(key, value);
        this.template.expire(key, expireTime, timeUnit);
        result = true;
    } catch (Exception var7) {
        var7.printStackTrace();
    }
    return result;
}
 
Example 18
Source File: CacheServiceProvider.java    From AsuraFramework with Apache License 2.0 4 votes vote down vote up
@Override
public void set(final String key, final String value) {
	final ValueOperations<String, String> operation = redisTemplate.opsForValue();
	operation.set(getKey(key), value);
}
 
Example 19
Source File: RedisUtil.java    From spring-boot-seckill with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 缓存value操作
 * @Author  科帮网
 * @param k
 * @param v
 * @param time
 * @param unit
 * @return  boolean
 * @Date	2017年12月23日
 * 更新日志
 * 2017年12月23日  科帮网  首次创建
 *
 */
public  boolean cacheValue(String k, Serializable v, long time,TimeUnit unit) {
    String key = KEY_PREFIX_VALUE + k;
    try {
        ValueOperations<Serializable, Serializable> valueOps =  redisTemplate.opsForValue();
        valueOps.set(key, v);
        if (time > 0) redisTemplate.expire(key, time, unit);
        return true;
    } catch (Throwable t) {
        logger.error("缓存[{}]失败, value[{}]",key,v,t);
    }
    return false;
}
 
Example 20
Source File: RedisCacheService.java    From DimpleBlog with Apache License 2.0 2 votes vote down vote up
/**
 * 缓存基本的对象,Integer、String、实体类等
 *
 * @param key      缓存的键值
 * @param value    缓存的值
 * @param timeout  时间
 * @param timeUnit 时间颗粒度
 * @return 缓存的对象
 */
public <T> ValueOperations<String, T> setCacheObject(String key, T value, Integer timeout, TimeUnit timeUnit) {
    ValueOperations<String, T> operation = redisTemplate.opsForValue();
    operation.set(key, value, timeout, timeUnit);
    return operation;
}