成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

使用Redis實現(xiàn)關(guān)注好友的功能

whatsns / 1891人閱讀

摘要:排名以開始,也就是說值最小的為。返回值返回成員排名,不存在返回取兩個集合的交集命令格式描述計算給定的一個或多個有序集的交集。其中給定的數(shù)量必須以參數(shù)指定,并將該交集結(jié)果集儲存到。返回值保存到的結(jié)果集成員數(shù)。

使用Redis實現(xiàn)關(guān)注好友的功能

現(xiàn)在很多社交都有關(guān)注或者添加粉絲的功能, 類似于這樣的功能我們?nèi)绻捎脭?shù)據(jù)庫做的話只是單純得到用戶的一些粉絲或者關(guān)注列表的話是很簡單也很容易實現(xiàn), 但是如果我想要查出兩個甚至多個用戶共同關(guān)注了哪些人或者想要查詢兩個或者多個用戶的共同粉絲的話就會很麻煩, 效率也不會很高. 但是如果你用redis去做的話就會相當(dāng)?shù)暮唵味倚屎芨? 原因是redis自己本身帶有專門針對于這種集合的交集,并集, 差集的一些操作。

設(shè)計思路如下:

? 總體思路我們采用redis里面的zset完成整個功能, 原因是zset有排序(我們要按照關(guān)注時間的倒序排列), 去重(我們不能多次關(guān)注同一用戶)功能. 一個用戶我們存貯兩個集合, 一個是保存用戶關(guān)注的人 另一個是保存關(guān)注用戶的人.
用到的命令是:
? 1、 zadd 添加成員:命令格式: zadd key score member [score …]
? 2、zrem 移除某個成員:命令格式: zrem key member [member ...]
? 3、 zcard 統(tǒng)計集合內(nèi)的成員數(shù):命令格式: zcard key
? 4、 zrange 查詢集合內(nèi)的成員:命令格式: ZRANGE key start stop [WITHSCORES]
? 描述:返回指定區(qū)間的成員。其中成員位置按 score 值遞增(從小到大)來排序。 WITHSCORES選項是用來讓成員和它的score值一并返回.
? 5、 zrevrange跟zrange作用相反
? 6、zrank獲取成員的排名:命令格式: zrank key member
? 描述:返回有序集key中成員member的排名。成員按 score 值遞增(從小到大)順序排列。排名以0開始,也就是說score 值最小的為0。返回值:返回成員排名,member不存在返回nil.
? 7、 zinterstore 取兩個集合的交集:命令格式:ZINTERSTORE destination numkeys key key ...] [AGGREGATE SUM|MIN|MAX]
? 描述:計算給定的一個或多個有序集的交集。其中給定 key 的數(shù)量必須以 numkeys 參數(shù)指定,并將該交集(結(jié)果集)儲存到 destination 。默認(rèn)情況下,結(jié)果集中某個成員的 score 值是所有給定集下該成員 score 值之 和 。
返回值:保存到 destination 的結(jié)果集成員數(shù)。

下面我用Java寫了一個簡單的例子 maven構(gòu)建

第一步: 添加Redis客戶端

    redis.clients
    jedis
    2.9.0
第二步: 封裝一個簡單的redis工具類
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    import redis.clients.jedis.JedisPoolConfig;
    public final class RedisUtil {
        //Redis服務(wù)器IP
        private static String ADDR = "localhost";
        //Redis的端口號
        private static int PORT = 6379;
        //訪問密碼
        private static String AUTH = "admin";
        //控制一個pool最多有多少個狀態(tài)為idle(空閑的)的jedis實例,默認(rèn)值也是8。
        private static int MAX_IDLE = 200;
        private static int TIMEOUT = 10000;
        //在borrow一個jedis實例時,是否提前進(jìn)行validate操作;如果為true,則得到的jedis實例均是可用的;
        private static boolean TEST_ON_BORROW = true;
        private static JedisPool jedisPool = null;
        
        static {
            try {
                JedisPoolConfig config = new JedisPoolConfig();
                config.setMaxIdle(MAX_IDLE);
                config.setTestOnBorrow(TEST_ON_BORROW);
                jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT, AUTH);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        public synchronized static Jedis getJedis() {
            try {
                if (jedisPool != null) {
                    Jedis resource = jedisPool.getResource();
                    return resource;
                } else {
                    return null;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
       
        @SuppressWarnings("deprecation")
        public static void returnResource(final Jedis jedis) {
            if (jedis != null) {
                jedisPool.returnResource(jedis);
            }
        }
    }
第四步: 封裝簡單的Follow類
    import java.util.HashSet;
    import java.util.Set;
    
    import redis.clients.jedis.Jedis;
    
    import com.indulgesmart.base.util.RedisUtil;
    public class FollowUtil {
    
        private static final String FOLLOWING = "FOLLOWING_";
        private static final String FANS = "FANS_";
        private static final String COMMON_KEY = "COMMON_FOLLOWING";
    
        // 關(guān)注或者取消關(guān)注
        public static int addOrRelease(String userId, String followingId) {
            if (userId == null || followingId == null) {
                return -1;
            }
            int isFollow = 0; // 0 = 取消關(guān)注 1 = 關(guān)注
            Jedis jedis = RedisUtil.getJedis();
            String followingKey = FOLLOWING + userId;
            String fansKey = FANS + followingId;
            if (jedis.zrank(followingKey, followingId) == null) { // 說明userId沒有關(guān)注過followingId
                jedis.zadd(followingKey, System.currentTimeMillis(), followingId);
                jedis.zadd(fansKey, System.currentTimeMillis(), userId);
                isFollow = 1;
            } else { // 取消關(guān)注
                jedis.zrem(followingKey, followingId);
                jedis.zrem(fansKey, fansKey);
            }
            return isFollow;
        }
    
        
         // 驗證兩個用戶之間的關(guān)系 
         // 0=沒關(guān)系  1=自己 2=userId關(guān)注了otherUserId 3= otherUserId是userId的粉絲 4=互相關(guān)注
        public int checkRelations (String userId, String otherUserId) {
    
            if (userId == null || otherUserId == null) {
                return 0;
            }
            
            if (userId.equals(otherUserId)) {
                return 1;
            }
            Jedis jedis = RedisUtil.getJedis();
            String followingKey = FOLLOWING + userId;
            int relation = 0;
            if (jedis.zrank(followingKey, otherUserId) != null) { // userId是否關(guān)注otherUserId
                relation = 2;
            }
            String fansKey = FANS + userId;
            if (jedis.zrank(fansKey, userId) != null) {// userId粉絲列表中是否有otherUserId
                relation = 3;
            }
            if ((jedis.zrank(followingKey, otherUserId) != null) 
                    && jedis.zrank(fansKey, userId) != null) {
                relation = 4;
            }
            return relation;
        }
    
        // 獲取用戶所有關(guān)注的人的id
        public static Set findFollwings(String userId) {
            return findSet(FOLLOWING + userId);
        }
    
        // 獲取用戶所有的粉絲
        public static Set findFans(String userId) {
            return findSet(FANS + userId);
        }
        
        // 獲取兩個共同關(guān)注的人
        public static Set findCommonFollowing(String userId, String otherUserId) {
            if (userId == null || otherUserId == null) {
                return new HashSet<>();
            }
            Jedis jedis = RedisUtil.getJedis();
            String commonKey = COMMON_KEY + userId + "_" + otherUserId;
            // 取交集
            jedis.zinterstore(commonKey + userId + "_" + otherUserId, FOLLOWING + userId, FOLLOWING + otherUserId); 
            Set result = jedis.zrange(commonKey, 0, -1);
            jedis.del(commonKey);
            return result;
        }
        
        // 根據(jù)key獲取set
        private static Set findSet(String key) {
            if (key == null) {
                return new HashSet<>();
            }
            Jedis jedis = RedisUtil.getJedis();
            Set result = jedis.zrevrange(key, 0, -1); // 按照score從大到小排序
            return result;
        }
    }

這是一點點小小的總結(jié), 希望能幫到要用到的人。

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/65365.html

相關(guān)文章

  • Redis 8 大應(yīng)用場景!

    摘要:之前講過的介紹,及使用帶來的優(yōu)勢,這章整理了一下的應(yīng)用場景,也是非常重要的,學(xué)不學(xué)得好,能正常落地是關(guān)鍵。下面一一來分析下的應(yīng)用場景都有哪些。提供的有序集合數(shù)據(jù)類構(gòu)能實現(xiàn)各種復(fù)雜的排行榜應(yīng)用。 之前講過Redis的介紹,及使用Redis帶來的優(yōu)勢,這章整理了一下Redis的應(yīng)用場景,也是非常重要的,學(xué)不學(xué)得好,能正常落地是關(guān)鍵。 下面一一來分析下Redis的應(yīng)用場景都有哪些。 1、緩存...

    CastlePeaK 評論0 收藏0
  • 使用redis實現(xiàn)互粉功能

    摘要:數(shù)據(jù)庫實現(xiàn)一下是數(shù)據(jù)庫的代碼,通過保存用戶的和關(guān)注對象的以及關(guān)注狀態(tài)來判斷用戶的關(guān)注列表和粉絲列表,通過聯(lián)查獲取用戶的基本信息,入頭像名稱。 使用redis實現(xiàn)互粉 最近在寫api的時候要實現(xiàn)一個相互關(guān)注的功能,發(fā)現(xiàn)如果用mysql做查詢不是很理想, 所以想能不能用redis來實現(xiàn)這個功能,網(wǎng)上一搜有很多實現(xiàn)的方法,結(jié)合網(wǎng)上的博文,實現(xiàn)了自己的功能。 1.數(shù)據(jù)庫實現(xiàn) 一下是數(shù)據(jù)庫的代...

    aikin 評論0 收藏0

發(fā)表評論

0條評論

whatsns

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<