redis的簡單使用執行個體

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   ar   color   os   使用   

1:在工程中匯入一下兩個jar包

2:

Javabean

package com.eoopen.xmpp.loginin;import java.io.Serializable;import java.util.Date;public class ClientUser implements Serializable{        /**     * 序列化     */    private static final long serialVersionUID = 2029803928604452271L;    public ClientUser() {    }        public ClientUser(String clientIdentify) {        super();        this.clientIdentify = clientIdentify;    }            /**     * 重寫hashcode     * clientIdentify相同就代表是同一個對象     */    @Override    public int hashCode() {        final int prime = 31;        int result = 1;        result = prime * result                + ((clientIdentify == null) ? 0 : clientIdentify.hashCode());        return result;    }    @Override    public boolean equals(Object obj) {        if (this == obj)            return true;        if (obj == null)            return false;        if (getClass() != obj.getClass())            return false;        ClientUser other = (ClientUser) obj;        if (clientIdentify == null) {            if (other.clientIdentify != null)                return false;        } else if (!clientIdentify.equals(other.clientIdentify))            return false;        return true;    }        private String id;    private String clientuserid;        /**     * 每個用戶端的唯一標識(如格式為111223_359776051195305),     * 該參數需要用戶端組裝後傳給服務端,     * 組裝方式為uid+裝置唯一標識,     * 該欄位最終作為用戶端登入openfire的帳號,     * 用戶端登陸的時候openfire的時候,帳號為clientIdentity,     * 密碼和用戶端登陸密碼一致     */    private String clientIdentify;        // 系統標識,如eim,lbs    private String serveridentify;        // 註冊openfire帳號時間    private Date createtime;    private Date updatetime;        // 來自哪個用戶端,ios,Android,pc    private String clienttype;        public String getClienttype() {        return clienttype;    }    public void setClienttype(String clienttype) {        this.clienttype = clienttype;    }    public Date getUpdatetime() {        return updatetime;    }    public void setUpdatetime(Date updatetime) {        this.updatetime = updatetime;    }    public String getServeridentify() {        return serveridentify;    }    public void setServeridentify(String serveridentify) {        this.serveridentify = serveridentify;    }    public Date getCreatetime() {        return createtime;    }    public void setCreatetime(Date createtime) {        this.createtime = createtime;    }    public String getId() {        return id;    }    public void setId(String id) {        this.id = id;    }    public String getClientuserid() {        return clientuserid;    }    public void setClientuserid(String clientuserid) {        this.clientuserid = clientuserid;    }    public String getClientIdentify() {        return clientIdentify;    }    public void setClientIdentify(String clientIdentify) {        this.clientIdentify = clientIdentify;    }}


(2)樣本

package com.eo.test.redis;import java.util.Set;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.SetOperations;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.eoopen.xmpp.loginin.ClientUser;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = "classpath:applicationContext.xml")public class TestZWJ extends AbstractJUnit4SpringContextTests{        @Autowired    private RedisTemplate redisTemplate;        @SuppressWarnings("unchecked")    @Test    public void testOfSet(){     // 建立一個set隊列
SetOperations<String, ClientUser> opsForSet = redisTemplate.opsForSet(); ClientUser u1 = new ClientUser("11"); ClientUser u2 = new ClientUser("12"); ClientUser u3 = new ClientUser("13"); ClientUser u4 = new ClientUser("14");      // 往隊列中添加4個使用者
opsForSet.add("66666666", u1); opsForSet.add("66666666", u2); opsForSet.add("66666666", u3); opsForSet.add("66666666", u4);     
     // 取出隊列中的所有對象
Set<ClientUser> members = opsForSet.members("66666666");      
     // 迴圈列印佇列中的所有對象
for (ClientUser clientUser : members) { System.out.println(clientUser.getClientIdentify()); }      // 刪除隊列中的一條資料
ClientUser u5 = new ClientUser("14"); members.remove(u5); for (ClientUser clientUser : members) { System.out.println(clientUser.getClientIdentify()); } Set<ClientUser> members2 = opsForSet.members("66666666"); for (ClientUser clientUser : members2) { System.out.println(clientUser.getClientIdentify()); } }}

 

 3:將資料庫中的資料全部放入redis緩衝中

public void initUserCacheQueue(){        RedisTemplate redisTemplate = (RedisTemplate) ContextUtil.getBean("redisTemplate");                //清空集合        redisTemplate.delete(Constant.OPENFIRE_USERS);        redisTemplate.delete(Constant.CLIENT_USERS);                ClientUserService userService = (ClientUserService) ContextUtil.getBean("clientUserService");                // 將所有的ClientUser資料放入redis緩衝中        List<ClientUser> clientUsers = userService.queryClientUser();        for (ClientUser clientUser : clientUsers) {            redisTemplate.opsForSet().add(Constant.CLIENT_USERS, clientUser);        }                // 將所有的OfUser資料放入redis緩衝中        List<OfUser> opeOfUsers = userService.queryOpenfireUser();        for (OfUser ofUser : opeOfUsers) {            redisTemplate.opsForSet().add(Constant.OPENFIRE_USERS, ofUser);        }    }

 

redis的簡單使用執行個體

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.