redis使用例子

來源:互聯網
上載者:User

標籤:

package test.iafclub.redis;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import java.util.UUID;import java.util.concurrent.TimeUnit;import net.sf.json.JSONArray;import net.sf.json.JSONObject;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.BoundHashOperations;import org.springframework.data.redis.core.BoundListOperations;import org.springframework.data.redis.core.BoundSetOperations;import org.springframework.data.redis.core.BoundValueOperations;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.iafclub.baseTools.util.MyDateUtil;import com.iafclub.demo.domain.Dictionary;@RunWith(SpringJUnit4ClassRunner.class)//配置了@ContextConfiguration註解並使用該註解的locations屬性指明spring和設定檔之後,//@ContextConfiguration(locations = {"classpath:spring.xml","classpath:spring-activitymq-test.xml", "classpath:spring-mybatis.xml", "classpath:dubbo.xml" })@ContextConfiguration(locations = {"classpath:spring.xml","classpath:spring-activitymq-test.xml", "classpath:spring-mybatis.xml", "classpath:dubbo.xml","classpath:spring-redis.xml" })public class RedisTest {    @Autowired    private StringRedisTemplate stringRedisTemplate;    @Autowired    private RedisTemplate redisTemplate;        private String REVERSE_KEY = "batchJob:task_";    private Dictionary initDictionary(String i){        Dictionary dictionary = new Dictionary();        dictionary.setId(UUID.randomUUID().toString());        dictionary.setTypeId("002");        dictionary.setTypeName("字典分類");        dictionary.setFieldKey("username"+i);        dictionary.setFieldValue("路人甲");        dictionary.setFieldBack("back1");        dictionary.setFieldBack2("back2");        dictionary.setFieldBack3("back3");        dictionary.setRemark("備忘"+i);        return dictionary;    }        /**Map插入*/    @Test    public void testMapPut(){        String key = REVERSE_KEY+"_testMap";        Map<String, String> newMap = new HashMap<String, String>();        stringRedisTemplate.boundHashOps(key).putAll(newMap);        BoundHashOperations<String, Object, Object> testMap = stringRedisTemplate.boundHashOps(key);        testMap.put("user2", JSONObject.fromObject(this.initDictionary("55555aa")).toString());        testMap.persist();    }        /**Map取值*/    @Test    public void testMapGet(){        String key = "testMap";        BoundHashOperations<String, Object, Object> testMap2 = stringRedisTemplate.boundHashOps(key);        System.out.println(testMap2.getExpire());        Dictionary dictionary = (Dictionary) JSONObject.toBean(JSONObject.fromObject(testMap2.get("user")), Dictionary.class);        System.out.println(dictionary.getFieldKey());    }        /**String 類型插入*/    @Test    public void testStringPut(){        for (int i=0;i<10;i++){            Dictionary dictionary = this.initDictionary(i+"");            String messageContent = JSONObject.fromObject(dictionary).toString();            System.out.println("發送訊息:" + messageContent);            String key = "USER_"+i;            stringRedisTemplate.boundValueOps(key).set(messageContent, 110,TimeUnit.DAYS);        }        List<Dictionary> dictionarys = new ArrayList<Dictionary>();        for (int i=0;i<10;i++){            Dictionary dictionary = this.initDictionary(i+"");            dictionarys.add(dictionary);        }        String messageContent = JSONArray.fromObject(dictionarys).toString();        System.out.println("發送訊息:" + messageContent);        BoundValueOperations<String, String> opt = stringRedisTemplate.boundValueOps("dictionarysList");        opt.set(messageContent, 110,TimeUnit.SECONDS);//        opt.set(messageContent);                System.out.println("發送完成");    }    /**String 類型取值     * 擷取指定的值*/    @Test    public void testStringGet(){         for (int i=0;i<1000;i++){            BoundValueOperations<String, String> opt = stringRedisTemplate.boundValueOps("dictionarysList");            try {                Thread.sleep(1000);            } catch (InterruptedException e) {            }            System.out.println(opt.getExpire());            System.out.println(opt.get());        }    }    /**String 類型取值     *擷取所有USER_開頭為key的值     * */        @Test    public void getSetValue(){         Set<String> keys = stringRedisTemplate.keys("USER_*");        System.out.println(keys);        for (String key : keys){            BoundValueOperations<String, String> opt = stringRedisTemplate.boundValueOps(key);            System.out.println(opt.getExpire());        }    }            /**刪除     * */        @Test    public void deleteValue(){ //        redisTemplate.delete("USER_*");        stringRedisTemplate.delete("dictionarysList");        System.out.println("0aaaaaaaaaaaaaaaaa");    }                /**刪除     * */        @Test    public void addValue(){ //        redisTemplate.delete("USER_*");        stringRedisTemplate.boundValueOps("dictionarysList").set("aa", 2, TimeUnit.MINUTES);        System.out.println("0aaaaaaaaaaaaaaaaa");    }    /**添加list     * */        @Test    public void testRedisTemplate(){ //        redisTemplate.delete("USER_*");        String key = "boundListOps";        BoundListOperations boundListOperations = redisTemplate.boundListOps(key);        System.out.println("=======a====="+boundListOperations);        boundListOperations.leftPush(this.initDictionary(System.currentTimeMillis()+""));        Object object = redisTemplate.boundListOps(key).leftPop();        System.out.println("=======b====="+JSONObject.fromObject(object));                System.out.println("\n\n\n\n\n\n\n\n\n\n");    }    /**添加list     * */        @Test    public void testRedisTemplateB(){         String key = "mygod";        int i=0;        while(true){            i++;            BoundListOperations boundListOperations = redisTemplate.boundListOps(key);            System.out.println("=======a====="+boundListOperations.size());            String keya = MyDateUtil.getCurrentDateTimeStr()+"00000000000"+i;            boundListOperations.leftPush(this.initDictionary(keya));                        BoundSetOperations boundSetOperations = redisTemplate.boundSetOps(key+"Set");            boundSetOperations.add(this.initDictionary(keya));                        if (i > 100){                break;            }//            Dictionary dictionary = (Dictionary) boundListOperations.rightPop();//            System.out.println(dictionary.getFieldKey());        }        System.out.println("\n\n\n\n\n\n\n\n\n\n");    }    @Test    public void testRedisListC(){         String key = "mygod";        BoundListOperations boundListOperations = redisTemplate.boundListOps(key);        System.out.println(boundListOperations.size());        while (boundListOperations.size() > 0){            System.out.println(boundListOperations.rightPop());        }    }        @Test    public void testRedisSet(){         String key = "mygodSet";        BoundSetOperations boundSetOperations = redisTemplate.boundSetOps(key);        System.out.println(boundSetOperations.size());        Set set = boundSetOperations.members();        for(Iterator itor = set.iterator();itor.hasNext();){            Dictionary d = (Dictionary) itor.next();            System.out.println(JSONObject.fromObject(d));        }    }}

 

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.