Redis(1)-用redis儲存商品-使用者關係,redis商品

來源:互聯網
上載者:User

Redis(1)-用redis儲存商品-使用者關係,redis商品

package com.lipeng;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.Date;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedHashMap;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.Set;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.dao.DataAccessException;import org.springframework.data.redis.core.HashOperations;import org.springframework.data.redis.core.RedisOperations;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.SessionCallback;public class UserSerivce {private RedisTemplate<String, Date> redisTemplate;HashOperations<String, String, Date> ho;public UserSerivce() {/** * 載入設定檔 */ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext-redis.xml");redisTemplate = (RedisTemplate) ac.getBean("redisTemplate");}/** * 使用者喜歡一個商品 *  * @param uId * @param pId */public void addLove(String uId, String pId) {// 使用者喜歡的商品增加一個final String uKey = getUKey(uId);final String uHashKey = pId + "";final String pKey = getPKey(pId);final String pHashKey = uId + "";final Date date = new Date();SessionCallback<Object> call = new SessionCallback<Object>() {public Object execute(RedisOperations ops) throws DataAccessException {// TODO Auto-generated method stubops.multi();ho = ops.opsForHash();ho.put(uKey, uHashKey, date);ho.put(pKey, pHashKey, date);ops.exec();return null;}};redisTemplate.execute(call);// 使用者喜歡數+1}/** * 取消愛好 *  * @param uId * @param pId */public void cannelLove(String uId, String pId) {final String uKey = getUKey(uId);final String pIdStr = pId;final String pKey = getPKey(pId);final String uIdStr = uId;SessionCallback<Object> call = new SessionCallback<Object>() {public Object execute(RedisOperations ops) throws DataAccessException {// TODO Auto-generated method stubops.multi();ho = ops.opsForHash();ho.delete(uKey, pIdStr);ho.delete(pKey, uIdStr);ops.exec();return null;}};redisTemplate.execute(call);}/** * 此使用者是否喜歡此商品 *  * @param uId * @param pId */public boolean isLoved(String uId, String pId) {boolean re = false;final String uKey = getUKey(uId);final String uHashKey = pId + "";ho = redisTemplate.opsForHash();System.out.println(uKey + "  ,   " + uHashKey);re = ho.hasKey(uKey, uHashKey);return re;}/** * 使用者喜歡商品的數兩 *  * @return */public Long getLoveCountByUid(String uId) {final String uKey = getUKey(uId);long re = 0;ho = redisTemplate.opsForHash();re = ho.size(uKey);return re;}/** * 商品的粉絲數 *  * @return */public Long getLoveCountByPid(String pId) {final String pKey = getPKey(pId);long re = 0;ho = redisTemplate.opsForHash();re = ho.size(pKey);return re;}/** * 刪除商品 */public void delP(String pId) {// 刪除商品的粉絲集合// 刪除使用者關注集合中的此商品final String pKey = getPKey(pId);final String pIdStr = pId + "";final Set<String> keys = redisTemplate.keys("uId:*:p");SessionCallback<Object> call = new SessionCallback<Object>() {public Object execute(RedisOperations ops) throws DataAccessException {// TODO Auto-generated method stubho = ops.opsForHash();ops.delete(pKey);if (keys != null && keys.size() > 0) {for (String key : keys) {if (ho.hasKey(key, pIdStr)) {//如果此使用者關注了該商品,則從關注列表中刪除,這個操作不能放到multi中,//因為multi中的操作都是暫時放到命令隊列中,到執行exec時統一提交,ho.hashKey()會返回null;System.out.println(pIdStr);ho.delete(key, pIdStr);}}}return null;}};redisTemplate.execute(call);}/** * 擷取商品的粉絲列表 *  * @param pId * @param count * @return */public Map<String, Date> getUidsByPid(String pId, int count) {Map<String, Date> map = null;final String pKey = getPKey(pId);SessionCallback<Map<String, Date>> call = new SessionCallback<Map<String, Date>>() {public Map<String, Date> execute(RedisOperations ops) throws DataAccessException {// TODO Auto-generated method stubops.multi();ho = ops.opsForHash();ops.exec();return (Map<String, Date>) ho.entries(pKey);}};map = redisTemplate.execute(call);return map;}/** * 擷取使用者的喜歡列表 *  * @param uId * @return */private Map<String, Date> getPidsByUid(String uId) {Map<String, Date> map = null;final String uKey = getUKey(uId);SessionCallback<Map<String, Date>> call = new SessionCallback<Map<String, Date>>() {public Map<String, Date> execute(RedisOperations ops) throws DataAccessException {// TODO Auto-generated method stubops.multi();ho = ops.opsForHash();ops.exec();return (Map<String, Date>) ho.entries(uKey);}};map = redisTemplate.execute(call);return map;}/** * 擷取使用者關注的商品ids *  * @param uId * @return */public List<String> getFocusPids(String uId) {return sortIds(this.getPidsByUid(uId));}/** * 擷取商品的粉絲IDS *  * @param pId * @return */public List<String> getFansUids(String pId) {return sortIds(this.getUidsByPid(pId, 0));}public static String getUKey(String uId) {return "uId:" + uId + ":p";}public static String getUCountKey(String uId) {return "uId:" + uId + ":pCount";}public static String getPKey(String pId) {return "pId:" + pId + ":u";}public static String getPCountKey(String pId) {return "pId:" + pId + ":uCount";}/** * 按時間進行排序 *  * @param map * @return */public List<String> sortIds(Map<String, Date> map) {List<Map.Entry<String, Date>> list = new LinkedList<Map.Entry<String, Date>>();list.addAll(map.entrySet());Collections.sort(list, new Comparator<Map.Entry<String, Date>>() {public int compare(Map.Entry<String, Date> date1, Map.Entry<String, Date> date2) {// 從高往低排序if (date1.getValue().getTime() < date2.getValue().getTime())return 1;if (date1.getValue().getTime() == date2.getValue().getTime())return 0;elsereturn -1;}});List<String> ids = new ArrayList<String>();for (Iterator<Map.Entry<String, Date>> ite = list.iterator(); ite.hasNext();) {Map.Entry<String, Date> map1 = ite.next();System.out.println("key-value: " + map1.getKey() + "," + map1.getValue());ids.add(map1.getKey());}return ids;}/** * 批量查詢商品的粉絲 *  * @param pids * @return key=pid value=fansCount */public Map<String, Long> getFansCountByPids(List<String> pids) {Map<String, Long> map = new HashMap<String, Long>();ho = redisTemplate.opsForHash();for (String pId : pids) {String pidKey = this.getPKey(pId);Long size = ho.size(pidKey);map.put(pId, size);}return map;}/** * 擷取粉絲數最多的count個商品id * @param count * @return */public Map<String,Long> getHot(int count){Map<String,Long>map=new HashMap<String,Long>();ho=redisTemplate.opsForHash();Set<String> keys=redisTemplate.keys("pId:*:u");if(keys!=null){for(String key:keys){map.put(key, ho.size(key));}}return sortByCount(map,count);}public Map<String,Long>  sortByCount(Map<String,Long> map,int count){Map<String,Long> re=new LinkedHashMap<String, Long>();List<Map.Entry<String, Long>> list = new ArrayList<Map.Entry<String, Long>>();list.addAll(map.entrySet());Collections.sort(list, new Comparator<Map.Entry<String, Long>>() {public int compare(Map.Entry<String, Long> date1, Map.Entry<String, Long> date2) {// 從高往低排序if (date1.getValue()< date2.getValue())return 1;if (date1.getValue() == date2.getValue())return 0;elsereturn -1;}});for (Iterator<Map.Entry<String, Long>> ite = list.iterator(); ite.hasNext();) {Map.Entry<String, Long> map1 = ite.next();System.out.println("key-value: " + map1.getKey() + "," + map1.getValue());re.put(map1.getKey() , map1.getValue());if(re.size()==count){return re;}}return re;}public static void main(String[] args) {UserSerivce us = new UserSerivce(); testAdds(us);// System.out.println(us.getLoveCountByPid(1));// System.out.println(us.getLoveCountByUid(3));// testCannelFocus(us);// testDel(us, uId, pId, list1, list2);// testGetFansCountByPids(us);//us.testMulti();Map<String, Long> map=us.getHot(2);System.out.println(map);}/** * 測試取消追蹤 *  * @param us */private static void testCannelFocus(UserSerivce us) {String uId = "0";String pId = "0";List<String> list1 = us.getFocusPids(uId);System.out.println("使用者" + uId + " 的關注有" + list1);System.out.println("使用者" + uId + "取消對商品" + pId + "的關注");us.cannelLove(uId, pId);List<String> list2 = us.getFocusPids(uId);System.out.println("使用者" + uId + " 的關注有" + list2);System.out.println(us.getLoveCountByPid("0"));}/** * 測試刪除商品 *  * @param us * @param uId * @param pId * @param list1 * @param list2 */private static void testDel(UserSerivce us, String uId, String pId, List<String> list1, List<String> list2) {System.out.println("商品" + pId + " 的粉絲有" + list1);System.out.println("使用者" + uId + " 的關注有" + list2);System.out.println("現在刪除商品" + pId);us.delP(pId);System.out.println("刪完之後");List<String> list3 = us.getFansUids(pId);List<String> list4 = us.getFocusPids(uId);System.out.println("商品" + pId + " 的粉絲有" + list3);System.out.println("使用者" + uId + " 的關注有" + list4);}/** * 測試添加商品 *  * @param us */private static void testAdds(UserSerivce us) {us.addLove("1", "1");us.addLove("2", "1");us.addLove("7", "3");us.addLove("8", "4");us.addLove("9", "4");us.addLove("1", "2");us.addLove("5", "3");us.addLove("4", "1");}/** * 測試批量擷取商品的粉絲數 *  * @param us */public static void testGetFansCountByPids(UserSerivce us) {List<String> pids = new ArrayList<String>();pids.add("1");pids.add("2");pids.add("3");pids.add("4");Map<String, Long> map = us.getFansCountByPids(pids);System.out.println(map);}public void testMulti() {redisTemplate.watch("a");redisTemplate.multi();redisTemplate.exec();redisTemplate.unwatch();}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.