iteye_2758 2012-03-31 12:01
浏览 258
已采纳

redis入门问题,求帮助

问题描述:模仿微博关注-粉丝 redis初步入门
[url]http://blog.nosqlfan.com/html/2938.html?ref=rediszt[/url]
参考那个PHP的例子,我这个是java的,没有达到预期效果。代码如下:
UserService.java接口类
[code="java"]package com.redis.test;

import java.util.Set;

public interface UserService {
// 关注用户
public void follow(String userId);

// 取消关注
public void unfollow(String userId);

// 关注了哪些人
public Set<String> following();

// 被哪些人关注(粉丝)
public Set<String> followedBy();

}[/code]
UserServiceImpl.java接口实现类
[code="java"]package com.redis.test;

import java.util.Set;

import redis.clients.jedis.Jedis;

public class UserServiceImpl implements UserService {

private String userId;
private Jedis redisService;

public UserServiceImpl(String userId) {
    this.userId = userId;
    this.redisService = new Jedis("localhost");
}

@Override
public void follow(String userId) {
    // 添加关注的人
    this.redisService.sadd("graph:user:{$this.userId}:following", userId);
    // 被关注的人添加
    this.redisService.sadd("graph:user:$userId:followed_by", this.userId);
}

@Override
public Set<String> following() {
    // 获取所关注的对象集合
    return this.redisService
            .smembers("graph:user:{$this.userId}:following");
}

@Override
public Set<String> followedBy() {
    // 获取被哪些编号进行关注
    return this.redisService
            .smembers("graph:user:{$this.userId}:followed_by");
}

@Override
public void unfollow(String userId) {
    // 移除关注列表
    this.redisService.srem("graph:user:{$this.userId}:following", userId);
    // 移除被关注列表/粉丝列表
    this.redisService.srem("graph:user:$userId:followed_by", this.userId);
}

}[/code]
客户端操作测试类
[code="java"]package com.redis.test;

import java.util.Iterator;

public class UserClient {

/**
 *@description 
 *@param args
 */
public static void main(String[] args) {
    UserService user1 = new UserServiceImpl("1");
    UserService user2 = new UserServiceImpl("2");
    UserService user3 = new UserServiceImpl("3");

    //user1关注的人有2 3
    user1.follow("2");
    user1.follow("3");

    Iterator<String> it1 = user1.following().iterator();
    System.out.println("user1 following:");
    while(it1.hasNext()){
        System.out.print(it1.next()+" "); //预计结果为2 3
        //实际为1 2 3
    }

    //user2关注1 3
    user2.follow("1");
    user2.follow("3");

    //user3关注1
    user3.follow("1");

    Iterator<String> it11 = user1.followedBy().iterator();
    System.out.println("\nuser1 followed_by:");
    while(it11.hasNext()){
        System.out.print(it11.next()+" "); //预计结果为2 3
        //实际为 无
    }

}

}[/code]
Windows系统、redis2.0.2版本、jedis客户端

这是什么问题呢。

  • 写回答

1条回答 默认 最新

  • Aronlulu 2012-04-01 11:16
    关注

    [code="java"]
    public Set followedBy() {

    // 获取被哪些编号进行关注

    return this.redisService

    .smembers("graph:user:{$this.userId}:followed_by");

    }

    [/code]
    "graph:user:{$this.userId}:followed_by"这个是key?
    $this.userId在php里面表示变量。在java里面"graph:user:{$this.userId}:followed_by"这整个就是字符串。
    所以获取不到对应key的set。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)