Jedis客户端使用——2
来源:互联网 发布:知画的孩子是谁的 编辑:程序博客网 时间:2024/06/15 22:44
在前面的例子中加入事务
package com.redis.DAO;import java.util.HashMap;import java.util.Map;import java.util.Random;import redis.clients.jedis.Jedis;import redis.clients.jedis.Transaction;import com.redis.bean.UserBean;import com.redis.storage.RedisPool;public class UserDAO {private static final String U_UID = "uid";private static final String U_KEY = "gen_sn";private static final String U_NAME = "name";private static final String U_PROFILE_PRE = "pr:";public UserBean getUserFromRedis(String genSN) throws Exception {UserBean userBean = null;Jedis jedis = null;try {jedis = RedisPool.getInstance().getConnection();Map<String, String> userProfile = null;userProfile = jedis.hgetAll(U_PROFILE_PRE + genSN);if (null != userProfile && userProfile.size() != 0) {userBean = new UserBean();userBean.setUid(Long.parseLong(userProfile.get(U_UID)));userBean.setKey(userProfile.get(U_KEY));userBean.setName(userProfile.get(U_NAME));}} catch (NumberFormatException e) {throw new Exception(e);} finally {RedisPool.getInstance().releaseConnetion(jedis);}return userBean;}@SuppressWarnings("null")public void addUserIntoRedis(UserBean userBean) throws Exception {Jedis jedis = null;Transaction transaction=null;try {jedis = RedisPool.getInstance().getConnection();transaction = jedis.multi();HashMap<String, String> profileMap = new HashMap<String, String>();profileMap.put(U_UID, Long.toString(userBean.getUid()));if (userBean.getKey() != null) {profileMap.put(U_KEY, userBean.getKey());}if (userBean.getName() != null) {profileMap.put(U_NAME, userBean.getName());}// 放入整个maptransaction.hmset(U_PROFILE_PRE + userBean.getKey(), profileMap);//50%几率抛异常Random r=new Random();int i=r.nextInt(2);if(i==0) {String sss=null;System.out.println(sss.length());}transaction.exec();} catch (Exception e) {transaction.discard(); throw new Exception(e);} finally {RedisPool.getInstance().releaseConnetion(jedis);}}}
package com.redis.service;import com.redis.DAO.UserDAO;import com.redis.bean.UserBean;import com.redis.storage.RedisPool;public class Main {public static void main(String[] args) {RedisPool.getInstance().Init();UserDAO dao=new UserDAO();UserBean user=new UserBean();user.setKey("test"); //唯一标识user.setUid(1000);user.setName("小高");try {dao.addUserIntoRedis(user);} catch (Exception e) {System.out.println(user+" 插入失败");}System.out.println("-----------------------6-----------------");UserBean result=null;try {result = dao.getUserFromRedis("test");} catch (Exception e) {System.out.println(user+" 获取失败");}System.out.println(result.getUid());System.out.println(result.getName());}}
- Jedis客户端使用——2
- Jedis客户端使用——1
- Redis客户端——Jedis使用
- jedis 客户端的使用
- Jedis cluster客户端使用
- Jedis cluster客户端使用
- Jedis客户端的使用
- jedis客户端对象的使用
- Redis之Jedis客户端使用
- redis客户端--Jedis的使用
- Redis 客户端 Jedis的使用
- redis 客户端 jedis的使用
- redis客户端jedis的简单使用
- Redis 客户端Jedis使用(一)
- try redis(四) -java 客户端jedis 使用
- redis的Java客户端jedis使用示例
- 使用Redis的Java客户端Jedis
- 使用Redis的Java客户端Jedis
- 数组查找
- echo
- ( ) HDU 4159 Indomie
- go - 变量和常量
- 直接把Jsp页面保存或导出Excel表格(最简单的导出Excel的方式)
- Jedis客户端使用——2
- printf() 函数压栈方式 从右到左压栈
- android php mysql json
- [转载]Windows下如何查看被占用的端口号及如何释放
- 上手ibatis!
- 推荐一个学习网站
- android源码编译环境,应用程序编译环境
- 一堆被称之为大学的东西——思想和精神(送给不仅仅是计算机专业的同学)
- 由某公司的面试小题目说起,对责任链模式的总结