@Service
public class FollowService {
@Autowired
JedisAdapter jedisAdapter;
* 用户关注了某个实体,可以关注问题,关注用户,关注评论等任何实体
* @param userId
* @param entityType
* @param entityId
* @return
*/
public boolean follow(int userId, int entityType, int entityId) {
String followerKey = RedisKeyUtil.getFollowerKey(entityType, entityId);
String followeeKey = RedisKeyUtil.getFolloweeKey(userId, entityType);
Date date = new Date();
Jedis jedis = jedisAdapter.getJedis();
Transaction tx = jedisAdapter.multi(jedis);
tx.zadd(followerKey, date.getTime(), String.valueOf(userId));
tx.zadd(followeeKey, date.getTime(), String.valueOf(entityId));
List<Object> ret = jedisAdapter.exec(tx, jedis);
return ret.size() == 2 && (Long) ret.get(0) > 0 && (Long) ret.get(1) > 0;
}
* 取消关注
* @param userId
* @param entityType
* @param entityId
* @return
*/
public boolean unfollow(int userId, int entityType, int entityId) {
String followerKey = RedisKeyUtil.getFollowerKey(entityType, entityId);
String followeeKey = RedisKeyUtil.getFolloweeKey(userId, entityType);
Date date = new Date();
Jedis jedis = jedisAdapter.getJedis();
Transaction tx = jedisAdapter.multi(jedis);
tx.zrem(followerKey, String.valueOf(userId));
tx.zrem(followeeKey, String.valueOf(entityId));
List<Object> ret = jedisAdapter.exec(tx, jedis);
return ret.size() == 2 && (Long) ret.get(0) > 0 && (Long) ret.get(1) > 0;
}
public List<Integer> getFollowers(int entityType, int entityId, int count) {
String followerKey = RedisKeyUtil.getFollowerKey(entityType, entityId);
return getIdsFromSet(jedisAdapter.zrevrange(followerKey, 0, count));
}
public List<Integer> getFollowers(int entityType, int entityId, int offset, int count) {
String followerKey = RedisKeyUtil.getFollowerKey(entityType, entityId);
return getIdsFromSet(jedisAdapter.zrevrange(followerKey, offset, offset+count));
}
public List<Integer> getFollowees(int userId, int entityType, int count) {
String followeeKey = RedisKeyUtil.getFolloweeKey(userId, entityType);
return getIdsFromSet(jedisAdapter.zrevrange(followeeKey, 0, count));
}
public List<Integer> getFollowees(int userId, int entityType, int offset, int count) {
String followeeKey = RedisKeyUtil.getFolloweeKey(userId, entityType);
return getIdsFromSet(jedisAdapter.zrevrange(followeeKey, offset, offset+count));
}
public long getFollowerCount(int entityType, int entityId) {
String followerKey = RedisKeyUtil.getFollowerKey(entityType, entityId);
return jedisAdapter.zcard(followerKey);
}
public long getFolloweeCount(int userId, int entityType) {
String followeeKey = RedisKeyUtil.getFolloweeKey(userId, entityType);
return jedisAdapter.zcard(followeeKey);
}
private List<Integer> getIdsFromSet(Set<String> idset) {
List<Integer> ids = new ArrayList<>();
for (String str : idset) {
ids.add(Integer.parseInt(str));
}
return ids;
}
* 判断用户是否关注了某个实体
* @param userId
* @param entityType
* @param entityId
* @return
*/
public boolean isFollower(int userId, int entityType, int entityId) {
String followerKey = RedisKeyUtil.getFollowerKey(entityType, entityId);
return jedisAdapter.zscore(followerKey, String.valueOf(userId)) != null;
}
}