org.springframework.data.redis.core.BoundHashOperations Java Examples

The following examples show how to use org.springframework.data.redis.core.BoundHashOperations. 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: CartService.java    From leyou with Apache License 2.0 6 votes vote down vote up
/**
 * 查询购物车列表
 *
 * @return
 */
public List<Cart> queryCartList() {
    // 获取登录用户
    UserInfo user = LoginInterceptor.getLoginUser();

    // 判断是否存在购物车
    String key = KEY_PREFIX + user.getId();
    if (!this.redisTemplate.hasKey(key)) {
        // 不存在,直接返回
        return null;
    }
    BoundHashOperations<String, Object, Object> hashOps = this.redisTemplate.boundHashOps(key);
    List<Object> carts = hashOps.values();
    // 判断是否有数据
    if (CollectionUtils.isEmpty(carts)) {
        return null;
    }
    // 查询购物车数据
    return carts.stream().map(o -> JsonUtils.parse(o.toString(), Cart.class)).collect(Collectors.toList());
}
 
Example #2
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 #3
Source File: AdminServiceImpl.java    From app-version with Apache License 2.0 6 votes vote down vote up
@Override
public ServiceResult deleteApp(int appId) {
    App app = appMapper.selectById(appId);
    if (app == null) {
        return ServiceResultConstants.APP_NOT_EXISTS;
    }
    app.setDelFlag(1);
    Integer integer = appMapper.updateById(app);
    if (integer > 0) {
        BoundHashOperations operations = redisTemplate.boundHashOps(RedisKey.APP_HASH);
        operations.delete(app.getTenantAppId());
        logger.info("删除应用[{}:{}]成功", app.getId(), app.getAppName());
        return ServiceResult.ok(app);
    } else {
        logger.error("删除应用[{}:{}]失败", app.getId(), app.getAppName());
        return ServiceResultConstants.DB_ERROR;
    }
}
 
Example #4
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 #5
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 #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");
}
 
Example #7
Source File: RedissonAutoConfigurationTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testApp() {
    redisson.getKeys().flushall();
    
    RMap<String, String> m = redisson.getMap("test", StringCodec.INSTANCE);
    m.put("1", "2");
    
    BoundHashOperations<String, String, String> hash = template.boundHashOps("test");
    String t = hash.get("1");
    assertThat(t).isEqualTo("2");
}
 
Example #8
Source File: BatchedRedisHashedListIterator.java    From synapse with Apache License 2.0 5 votes vote down vote up
@Override
public boolean hasNext() {
    if (next == null) {
        if (messageListIterator.hasNext()) {
            final String messageHashKey = messageListIterator.next();
            final BoundHashOperations<String, String, String> hashOps = redisTemplate.boundHashOps(messageHashKey);
            final Map<String, String> entries = hashOps.entries();
            if (entries != null && !entries.isEmpty()) {
                next = valueTransformer.apply(entries);
            }
        }
    }
    return next != null;
}
 
Example #9
Source File: RegController.java    From push with Apache License 2.0 5 votes vote down vote up
@Scheduled(cron = "0/30 * * * * *")
public void heartbeat() {
    logger.debug("heartbeat");
    BoundHashOperations<String, String, ServerNode> imKeep = getNodes();
    for (Map.Entry<String, ServerNode> node : imKeep.entries().entrySet()) {
        ServerNode serverNode = node.getValue();
        if (System.currentTimeMillis() - serverNode.getLastCheckTime() > 70 * 1000) {
            imKeep.delete(node.getKey());
            logger.debug("heartbeat delete:{} {}", node.getKey(), System.currentTimeMillis() - serverNode.getLastCheckTime());
        }
    }
}
 
Example #10
Source File: RegController.java    From push with Apache License 2.0 5 votes vote down vote up
@GetMapping("nodes")
public Set<String> nodes() {
    BoundHashOperations<String, String, ServerNode> imKeep = getNodes();
    Set<String> set = new HashSet<>();
    for (ServerNode node : Objects.requireNonNull(imKeep.values())) {
        set.add(node.getUrl());
    }
    return set;
}
 
Example #11
Source File: RedisCacheManager.java    From ssm with Apache License 2.0 5 votes vote down vote up
@Override
public V remove(K key) throws CacheException {
	BoundHashOperations<String,K,V> hash = redisTemplate.boundHashOps(cacheKey);

	Object k=hashKey(key);
	V value=hash.get(k);
	hash.delete(k);
	return value;
}
 
Example #12
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 #13
Source File: AdminServiceImpl.java    From app-version with Apache License 2.0 5 votes vote down vote up
@Override
public ServiceResult editApp(int appId, String appName, String tenantAppId) {
    App query = new App();
    query.setAppName(appName);
    query.setDelFlag(0);
    query.setTenantAppId(tenantAppId);

    EntityWrapper<App> wrapper = new EntityWrapper<>();
    wrapper.andNew().eq("app_name", appName).or().eq("tenant_app_id", tenantAppId);
    wrapper.andNew().eq("del_flag", 0);
    wrapper.and().ne("id", appId);
    List<App> apps = appMapper.selectList(wrapper);
    if (!apps.isEmpty()) {
        return new ServiceResult(
                ServiceResultConstants.APP_EXISTS.getCode(),
                "已经有其他应用取名为[" + appName + "],或AppId为[ " + tenantAppId + " ],请换一个名称或AppId");
    }
    App app = new App();
    app.setId(appId);
    app.setAppName(appName);
    app.setTenantAppId(tenantAppId);
    Integer integer = appMapper.updateById(app);
    if (integer > 0) {
        BoundHashOperations operations = redisTemplate.boundHashOps(RedisKey.APP_HASH);
        App app2 = appMapper.selectById(appId);
        if (app2 != null) {
            operations.delete(app2.getTenantAppId());
        }
        logger.info("修改App[{}]成功", appName);
        return ServiceResult.ok(app);
    } else {
        logger.error("修改App[{}]失败", appName);
        return ServiceResultConstants.DB_ERROR;
    }
}
 
Example #14
Source File: CartService.java    From leyou with Apache License 2.0 5 votes vote down vote up
/**
 * 删除购物车中的商品
 *
 * @param skuId
 */
public void deleteCart(String skuId) {
    // 获取登录用户
    UserInfo user = LoginInterceptor.getLoginUser();
    String key = KEY_PREFIX + user.getId();
    BoundHashOperations<String, Object, Object> hashOps = this.redisTemplate.boundHashOps(key);
    hashOps.delete(skuId);
}
 
Example #15
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 #16
Source File: RedisCacheManager.java    From ssm with Apache License 2.0 4 votes vote down vote up
@Override
public int size() {
	BoundHashOperations<String,K,V> hash = redisTemplate.boundHashOps(cacheKey);
	return hash.size().intValue();
}
 
Example #17
Source File: RedisCacheManager.java    From ssm with Apache License 2.0 4 votes vote down vote up
@Override
public Set<K> keys() {
	BoundHashOperations<String,K,V> hash = redisTemplate.boundHashOps(cacheKey);
	return hash.keys();
}
 
Example #18
Source File: RedisCacheManager.java    From ssm with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<V> values() {
	BoundHashOperations<String,K,V> hash = redisTemplate.boundHashOps(cacheKey);
	return hash.values();
}
 
Example #19
Source File: RedisSessionRegistry.java    From albedo with GNU Lesser General Public License v3.0 4 votes vote down vote up
public Set<String> putIfAbsentPrincipals(Object principal, final Set<String> set) {
	UserDetail userDetail = (UserDetail) principal;
	BoundHashOperations<String, String, Set<String>> hashOperations = redisTemplate.boundHashOps(PRINCIPALS);
	hashOperations.putIfAbsent(userDetail.getId(), set);
	return hashOperations.get(userDetail.getId());
}
 
Example #20
Source File: UserManager.java    From push with Apache License 2.0 4 votes vote down vote up
public BoundHashOperations<String, String, User> getUserHash() {
    return redisTemplate.boundHashOps(onlineUserListKey);
}
 
Example #21
Source File: SessionManager.java    From push with Apache License 2.0 4 votes vote down vote up
public BoundHashOperations<String, String, String> getCache() {
    return redisTemplate.boundHashOps(cacheKey);
}
 
Example #22
Source File: RedisCacheManager.java    From ssm with Apache License 2.0 4 votes vote down vote up
@Override
public V get(K key) throws CacheException {
	BoundHashOperations<String,K,V> hash = redisTemplate.boundHashOps(cacheKey);
	Object k=hashKey(key);
	return hash.get(k);
}
 
Example #23
Source File: SysUserServiceImpl.java    From spring-admin-vue with Apache License 2.0 4 votes vote down vote up
private BoundHashOperations<String, String, Object> tokenStorage() {
    return redisTemplate.boundHashOps(jwtTokenUtils.getTokenHeader());
}
 
Example #24
Source File: RegController.java    From push with Apache License 2.0 4 votes vote down vote up
private BoundHashOperations getNodes() {
    BoundHashOperations imKeep = redisTemplate.boundHashOps("im_keep");
    return imKeep;
}
 
Example #25
Source File: MyRedisTemplate.java    From redis-admin with Apache License 2.0 4 votes vote down vote up
@Override
public <HK, HV> BoundHashOperations<K, HK, HV> boundHashOps(K key) {
	throw new MethodNotSupportException("myRedisTemplate not support this method : boundHashOps(K key) , please use opsForXX");
	//return new DefaultBoundHashOperations<K, HK, HV>(key, this);
}
 
Example #26
Source File: RedisJobPool.java    From Milkomeda with MIT License 4 votes vote down vote up
private BoundHashOperations<String, String, String> getPool () {
    return redisTemplate.boundHashOps(jobPoolKey);
}
 
Example #27
Source File: RedisIndexedSessionRepository.java    From spring-session with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the {@link BoundHashOperations} to operate on a {@link Session}.
 * @param sessionId the id of the {@link Session} to work with
 * @return the {@link BoundHashOperations} to operate on a {@link Session}
 */
private BoundHashOperations<Object, Object, Object> getSessionBoundHashOperations(String sessionId) {
	String key = getSessionKey(sessionId);
	return this.sessionRedisOperations.boundHashOps(key);
}