local food=redis.call('hget',KEYS[1],'food'); food=food+ARGV[1]; redis.call('hset',KEYS[1],'food',food); local diamond=redis.call('hget',KEYS[1],'diamond'); diamond=diamond+ARGV[2]; redis.call('hset',KEYS[1],'diamond',diamond);
Jedis jedis = new Jedis("192.168.128.128", 6379); //1. Initial player data to Redis GamePlayer player = new GamePlayer(); player.setId(1001); player.setName("ksfzhaohui"); player.setFood(100); player.setDiamond(100); Map<String, String> beanMap = BeanUtil.warp(player);// Convert object to map String beanKey = getRedisBeanKey(player.getClass(), player.getId()); System.out.println("key:" + beanKey); Jedis. hmset (beanKey, beanMap);//Save player data to Redis
String script = "local food=redis.call('hget',KEYS[1],'food'); " + "food=food+ARGV[1];" + "redis.call('hset',KEYS[1],'food',food);" + "local diamond=redis.call('hget',KEYS[1],'diamond'); " + "diamond=diamond+ARGV[2];" + "redis.call('hset',KEYS[1],'diamond',diamond);"; List<String> keys = new ArrayList<String>(); keys.add(beanKey); List<String> args = new ArrayList<String>(); args.add("100"); args.add("100"); //3. Execute script jedis.eval(script, keys, args);
public class BeanUtil { private static Logger logger = Logger.getLogger(BeanUtil.class); private static final String CLASS = "class"; /** *Encapsulates the specified object data into a map * * @param bean *Object Data * @return */ @SuppressWarnings("all") public static Map<String, String> warp(Object bean) { Map<String, String> propertyMap = new HashMap<String, String>(); try { PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()) .getPropertyDescriptors(); for (PropertyDescriptor propertyDescriptor : ps) { String propertyName = propertyDescriptor.getName(); if (propertyName != null && !propertyName.equals(CLASS)) { Method getter = propertyDescriptor.getReadMethod(); if (getter != null) { propertyMap.put(propertyName, String.valueOf(getter.invoke(bean, null))); } } } } catch (Exception e) { logger.error(e); } return propertyMap; } }