memcached整合springboot

整合

依賴
    <dependency>
            <groupId>com.googlecode.xmemcached</groupId>
            <artifactId>xmemcached</artifactId>
            <version>2.4.5</version>
        </dependency>
配置

application.yml配置如下

memcached:
  //memcached服務(wù)器節(jié)點
  servers: 192.168.139.145:2222 192.168.139.145:6666
  //nio連接池的數(shù)量
  poolSize: 10
  //是否啟用url  encode機制
  sanitizeKeys: false

構(gòu)建一個config bean坪郭,對應(yīng)配置文件內(nèi)容

@Component
@ConfigurationProperties(prefix = "memcached")
public class XMemcachedProperties {

    private String servers;
    private Integer poolSize;
    private Boolean  santizeKeys;
    //省略getter-setter
}

構(gòu)造MemcachedClient

  • 構(gòu)造MemcachedClient Bean之前啤它,需要先構(gòu)造MemcachedClientBuilder
    @Autowired
    private XMemcachedProperties properties;

    //構(gòu)建builder
    @Bean
    public MemcachedClientBuilder getXMBuilder(){
        MemcachedClientBuilder memcachedClientBuilder = null;
         try {
            String server =properties.getServers();
            memcachedClientBuilder= new XMemcachedClientBuilder(server);

            memcachedClientBuilder.setFailureMode(false);
            memcachedClientBuilder.setSanitizeKeys(properties.getSantizeKeys());
            memcachedClientBuilder.setConnectionPoolSize(properties.getPoolSize());
            memcachedClientBuilder.setCommandFactory(new BinaryCommandFactory());
            memcachedClientBuilder.setOpTimeout(3000);
            memcachedClientBuilder.setSessionLocator(new KetamaMemcachedSessionLocator());
            return  memcachedClientBuilder;
         }catch (Exception e){
            e.printStackTrace();
         }
         return null;
    }
  • 構(gòu)造MemcachedClient

    @Bean
    public MemcachedClient getXMClient(MemcachedClientBuilder builder){
        MemcachedClient client = null;
        try {
            client = builder.build();
            return client;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

測試

  • 編寫一個Service,注入MemcachedClient ,調(diào)用set/get命令

@Service
public class ShowService {

    @Autowired
    private MemcachedClient memcachedClient;


    public void showAdd(String key,String value){
        try {
            memcachedClient.set(key,3600,value);
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    public String showQuery(String key){
        try {
            return  memcachedClient.get(key);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }

}
  • 編寫junit
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

    @Autowired
    private ShowService showService;

    @Test
    public void contextLoads() {

        String key = "k1";
        String value= "k1value";

        String key2 = "k2";
        String value2= "k2value";
        showService.showAdd(key,value);
        showService.showAdd(key2,value2);
        System.out.println(showService.showQuery(key));
        System.out.println(showService.showQuery(key2));
    }

}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末刽射,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌八秃,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件肉盹,死亡現(xiàn)場離奇詭異昔驱,居然都是意外死亡,警方通過查閱死者的電腦和手機上忍,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進店門骤肛,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人窍蓝,你說我怎么就攤上這事腋颠。” “怎么了吓笙?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵淑玫,是天一觀的道長。 經(jīng)常有香客問我,道長絮蒿,這世上最難降的妖魔是什么尊搬? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮土涝,結(jié)果婚禮上佛寿,老公的妹妹穿的比我還像新娘。我一直安慰自己但壮,他們只是感情好冀泻,可當我...
    茶點故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著茵肃,像睡著了一般腔长。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上验残,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天捞附,我揣著相機與錄音,去河邊找鬼您没。 笑死鸟召,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的氨鹏。 我是一名探鬼主播欧募,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼仆抵!你這毒婦竟也來了跟继?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤镣丑,失蹤者是張志新(化名)和其女友劉穎舔糖,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體莺匠,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡金吗,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了趣竣。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片摇庙。...
    茶點故事閱讀 40,090評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖遥缕,靈堂內(nèi)的尸體忽然破棺而出卫袒,到底是詐尸還是另有隱情,我是刑警寧澤单匣,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布夕凝,位于F島的核電站烤蜕,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏迹冤。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一虎忌、第九天 我趴在偏房一處隱蔽的房頂上張望泡徙。 院中可真熱鬧,春花似錦膜蠢、人聲如沸堪藐。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽礁竞。三九已至,卻和暖如春杉辙,著一層夾襖步出監(jiān)牢的瞬間模捂,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工蜘矢, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留狂男,地道東北人。 一個月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓品腹,卻偏偏與公主長得像岖食,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子舞吭,可洞房花燭夜當晚...
    茶點故事閱讀 45,033評論 2 355

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理泡垃,服務(wù)發(fā)現(xiàn),斷路器羡鸥,智...
    卡卡羅2017閱讀 134,659評論 18 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,822評論 6 342
  • 1.1 Spring IoC容器和bean簡介 本章介紹了Spring Framework實現(xiàn)的控制反轉(zhuǎn)(IoC)...
    起名真是難閱讀 2,584評論 0 8
  • 1.1 spring IoC容器和beans的簡介 Spring 框架的最核心基礎(chǔ)的功能是IoC(控制反轉(zhuǎn))容器蔑穴,...
    simoscode閱讀 6,717評論 2 22
  • “在有生的歲月能遇到你,竟花光所有運氣”—《明年今日》 “我想去看海兄春,你能陪我嗎澎剥?” 木荊在還有兩個小時的車程上給...
    加幾筆閱讀 173評論 0 0