关于如何从Redis中取出自增值的方法
2018-01-11 20:18
120 查看
此方法适用于想要从redis中获取一个全局(分布式)唯一的数字,一般结合时间戳使用,可以拼接在一起作为一个本机唯一标识.代码如下:(测试代码也在后面)
//测试代码
package *****; import java.io.Serializable; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import com.kinglong.p2p.constant.Constant; /** * redis全剧唯一数字业务实现类 *ClassName:UniqueRedisNumberServiceImpl *<p>Desc:</p> * @author */ @Service public class UniqueRedisNumberServiceImpl implements UniqueRedisNumberService { @Autowired private RedisTemplate<String, Serializable> redisTemplate; /** * 获取redis全局唯一数字 */ @Override public long getUniqueRedisNumber() { //注意:1L表示取得的值为长整型,并且他们是每次递增1个.如果为2L,则表示每次递增2 long result = redisTemplate.opsForValue().increment(Constant.UNIQUE_NUMBER,1L); return result; } }
//测试代码
package ***; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.kinglong.p2p.service.loan.BidInfoService; import com.kinglong.p2p.service.loan.UniqueRedisNumberService; public class Test02 { public static void main(String[] args) { //加载spring框架容器是测试方法的核心,一定不能漏了 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); BidInfoService bidInfoService = (BidInfoService) context.getBean("bidInfoServiceImpl"); UniqueRedisNumberService uniqueRedisNumberService = (UniqueRedisNumberService) context.getBean("uniqueRedisNumberServiceImpl"); //准备请求参数 long uniqueRedisNumber = uniqueRedisNumberService.getUniqueRedisNumber(); System.out.println(uniqueRedisNumber); } }
相关文章推荐
- 关于BS程序开发中如何防止页面被多次提交解决方法!
- *关于如何得到查询记录的条数的方法^_^*
- 关于服务器如何设置动易系统数据库路径的方法
- 如何从数据库中随机取出10条记录的方法
- 关于如何在已有库中利用现有表的id 重置oracle表的sequence的方法
- 如何从数据库中随机取出10条记录的方法
- 关于如何操作其他窗体的控件或变量的方法
- 关于如何制作Rss的一种简单方法!
- 关于如何制作Rss的一种简单方法![转]
- 关于struts如何在action中实现多个处理方法
- 关于软件防止破解的思考,如何避免简单的跳转指令型的验证方法,如何设置更复杂的验证方式。
- 关于C++中如何判断文件,目录存在的若干方法
- blog流量分析以及关于如何提高CSDN blog访问数量的初步方法
- 关于对象数据库 DB4O 的一些BUG以及如何应对的方法
- *关于如何获取指定表的指定字段的数据类型的方法
- 关于如何找到vmx文件(虚拟系统配置文件)的方法
- 关于客户端如何获取服务器时间的方法
- 关于如何制作Rss的一种简单方法
- 关于如何操作其他窗体的控件或变量的方法!
- 关于"如何让应用程序只有一个实例在运行"的孟宪会的解决方法的疑问