点賛実現[多対多のredis実現]

1147 ワード

点賛実現:
構想:questionごとにset()を対応する;jedisを呼び出すsadd(key,value); question-賛userIdは多対多のデータテーブル構造であるためset構造
拡張:各記事には異なるラベルがあり、このデータ構造で実現することもできます.
ビジネス層:
@Serveice

//  userId,              
public long like(int userId,int entityType,int entityId){
    //         
    String likeKey= RedisKeyUtil.getLikeKey(entityType, entityId);
    jedisAdapter.sadd(likeKey,String.valueOf(userId));
}

データ層
@Service//       ,       implements InitializingBean  pool
public class JedisAdapter implements InitializingBean{

//   pool,            
    @Override
    public void afterPropertiesSet() throws Exception {
        pool=new JedisPool("redis://localhost:6379/10");
    }

 //          ;              
    public long sadd(String key,String value){
        Jedis jedis=null;
        try{
            jedis=pool.getResource();
            return jedis.sadd(key,value);
        }catch (Exception e){
            logger.error("    :"+e.getMessage());
        }finally {
            if (jedis!=null){
                jedis.close();
            }
        }
        return 0;
    }   
}