Java Code Examples for org.springframework.data.redis.core.BoundHashOperations#put()

The following examples show how to use org.springframework.data.redis.core.BoundHashOperations#put() . 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: RedisTest.java    From leyou with Apache License 2.0 6 votes vote down vote up
@Test
public void testHash() {
    BoundHashOperations<String, Object, Object> hashOps =
            this.redisTemplate.boundHashOps("user");
    // 操作hash数据
    hashOps.put("name", "jack");
    hashOps.put("age", "21");

    // 获取单个数据
    Object name = hashOps.get("name");
    System.out.println("name = " + name);

    // 获取所有数据
    Map<Object, Object> map = hashOps.entries();
    for (Map.Entry<Object, Object> me : map.entrySet()) {
        System.out.println(me.getKey() + " : " + me.getValue());
    }
}
 
Example 2
Source File: RegController.java    From push with Apache License 2.0 6 votes vote down vote up
@PostMapping("keep")
public ServerNode keep(@RequestParam String id, @RequestParam(required = false) String ip, @RequestParam String port, HttpServletRequest request) {
    BoundHashOperations imKeep = getNodes();
    ServerNode serverNode = null;
    if (!imKeep.hasKey(id)) {
        serverNode = new ServerNode();
        serverNode.setId(id);
        if (StringUtils.isEmpty(ip))
            ip = IpUtil.getIpAddr(request);
        serverNode.setUrl(ip + ":" + port);
        serverNode.setLastCheckTime(System.currentTimeMillis());
    } else {
        serverNode = (ServerNode) imKeep.get(id);
        serverNode.setLastCheckTime(System.currentTimeMillis());
    }
    logger.debug("keep:{} {} {}", id, ip, port);
    imKeep.put(id, serverNode);
    return serverNode;
}
 
Example 3
Source File: CartService.java    From leyou with Apache License 2.0 5 votes vote down vote up
/**
 * 添加购物车
 *
 * @param cart
 */
public void addCart(Cart cart) {
    // 获取登录用户
    UserInfo user = LoginInterceptor.getLoginUser();
    // Redis的key
    String key = KEY_PREFIX + user.getId();
    // 获取hash操作对象
    BoundHashOperations<String, Object, Object> hashOps = this.redisTemplate.boundHashOps(key);
    // 查询是否存在
    Long skuId = cart.getSkuId();
    Integer num = cart.getNum();
    Boolean boo = hashOps.hasKey(skuId.toString());
    if (boo) {
        // 存在,获取购物车数据
        String json = hashOps.get(skuId.toString()).toString();
        cart = JsonUtils.parse(json, Cart.class);
        // 修改购物车数量
        cart.setNum(cart.getNum() + num);
    } else {
        // 不存在,新增购物车数据
        cart.setUserId(user.getId());
        // 其它商品信息,需要查询商品服务
        Sku sku = this.goodsClient.querySkuById(skuId);
        cart.setImage(StringUtils.isBlank(sku.getImages()) ? "" : StringUtils.split(sku.getImages(), ",")[0]);
        cart.setPrice(sku.getPrice());
        cart.setTitle(sku.getTitle());
        cart.setOwnSpec(sku.getOwnSpec());
    }
    // 将购物车数据写入redis
    hashOps.put(cart.getSkuId().toString(), JsonUtils.serialize(cart));
}
 
Example 4
Source File: CartService.java    From leyou with Apache License 2.0 5 votes vote down vote up
/**
 * 修改购物车数量
 *
 * @param cart
 */
public void updateCarts(Cart cart) {
    // 获取登陆信息
    UserInfo userInfo = LoginInterceptor.getLoginUser();
    String key = KEY_PREFIX + userInfo.getId();
    // 获取hash操作对象
    BoundHashOperations<String, Object, Object> hashOperations = this.redisTemplate.boundHashOps(key);
    // 获取购物车信息
    String cartJson = hashOperations.get(cart.getSkuId().toString()).toString();
    Cart cart1 = JsonUtils.parse(cartJson, Cart.class);
    // 更新数量
    cart1.setNum(cart.getNum());
    // 写入购物车
    hashOperations.put(cart.getSkuId().toString(), JsonUtils.serialize(cart1));
}
 
Example 5
Source File: RedisCacheManager.java    From ssm with Apache License 2.0 5 votes vote down vote up
@Override
public V put(K key, V value) throws CacheException {
	BoundHashOperations<String,K,V> hash = redisTemplate.boundHashOps(cacheKey);
	Object k=hashKey(key);
	hash.put((K)k, value);
	return value;
}
 
Example 6
Source File: RedisEnvironmentRepositoryIntegrationTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void test() {
	BoundHashOperations bound = redis.boundHashOps("foo-bar");
	bound.put("name", "foo");
	bound.put("tag", "myapp");

	Environment env = new RedisEnvironmentRepository(redis,
			new RedisEnvironmentProperties()).findOne("foo", "bar", "");
	assertThat(env.getName()).isEqualTo("foo");
	assertThat(env.getPropertySources()).isNotEmpty();
	assertThat(env.getPropertySources().get(0).getSource().get("tag"))
			.isEqualTo("myapp");
}