Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Javaユーザに伝えたいGuava Cache入門

Javaユーザに伝えたいGuava Cache入門

Hiroto YAMAKAWA

January 15, 2017
Tweet

More Decks by Hiroto YAMAKAWA

Other Decks in Technology

Transcript

  1. 

  2. αϯϓϧɿΩʔΛ༩͑ΔͱࣗಈతʹΩϟογϡσʔλΛੜ੒ LoadingCache<String, Integer> randomIntCache = CacheBuilder.newBuilder() .expireAfterWrite(5, TimeUnit.SECONDS) .build(new CacheLoader<String,

    Integer>() { @Override public Integer load(String key) throws Exception { return getRandomInt(); }
 }); HFU ǒGPPǒ ӑѬMPBE ǒGPPǒ Әᇴ⷗✿᧠ӼǒGPPǒӒҮҰԕդӑԕՋԫԟՍӃӵѭ ⓬ᒁӉӍӉӳѬԕՋԫԟՍӁӉԌ՛ԡԧ՛ԡӼ⏺᪨Ӄӵ Integer v1 = randomIntCache.get(”foo”); ← (1) loadϝιουΛ࣮ߦ͠ɺ݁ՌΛΩϟογϡ Integer v2 = randomIntCache.get(”foo”); ← (2) (1)ͷ࣮ߦ݁Ռͱશ͘ಉ͡ΠϯελϯεΛฦ͢ 
  3. LoadingCache<String, Integer> randomIntCache = CacheBuilder.newBuilder() .expireAfterWrite(5, TimeUnit.SECONDS) .build(new CacheLoader<String, Integer>()

    { @Override public Integer load(String key) throws Exception { return getRandomInt(); }
 }); ԕՋԫԟՍӘ⻎ᠶᦴঽӔӓ LFZ 7BMVF ԕՋԫԟՍӃӵ7BMVFྞԌ՛ԡԧ՛ԡӘ⇰ᘻᢅᴿѭ,FZӼਜӍӉ௡ⅈӑ⇰ᘻӬ൧⥎ѭ ,FZ 7BMVFәԗՑԡѭ
 $BDIF-PBEFSәѬ,FZҾӒӕԕՋԫԟՍӃӵ7BMVFԌ՛ԡԧ՛ԡӘ⇰ᘻᢅᴿӼఙᒐӃӵѭ ԕՋԫԟՍӘ⻎ᠶᦴঽӔӓә⹋ᡔ⼀ᇰӑҷӵѭ㮢ఙ㖟ᣪ㔿ѬԕդӘᡔӔӓ㮣 $BDIF-PEFSӼ ਜҰӒҷә -PBEJOH$BDIF αϯϓϧɿΩʔΛ༩͑ΔͱࣗಈతʹΩϟογϡσʔλΛੜ੒ LFZ 7BMVF LFZ 7BMVF 
  4. αϯϓϧɿΩʔΛ༩͑ΔͱࣗಈతʹΩϟογϡσʔλΛੜ੒ LoadingCache<String, Integer> randomIntCache = CacheBuilder.newBuilder() .expireAfterWrite(5, TimeUnit.SECONDS) .build(CacheLoader.from(() ->

    getRandomInt())); +BWBӘMBNCEBᐛ㮢'VODUJPO 4VQQMJFS㮣ӼਜӍӐ$BDIF-PBEFSӼਃӶӵѭѫ 'VODUJPO LFZ LFZӼਜҰ௡ⅈ 4VQQMJFS LFZӼਜӹӔҮ௡ⅈ -BNCEBᐛӼਜҰ࿾൶Ѭ&YDFQUJPOҶԡՕդҿӶӔҮӘӑѬHFU6ODIFDLFE ,FZ ӼਜҰѭ
 㮢⼳✦әᒁ㈨ѭӉӊӁHFUӘӨӨӑӬහӝ௲Ӂ୤ӕਦႌ௡ⅈҶᒨ⺙ӔҽӒলႌәใ㛸әᾩҮ㮣 Integer v1 = randomIntCache.getUnchecked(”foo”); ← ExceptionͷνΣοΫΛ͠ͳ͍৔߹ Integer v2 = randomIntCache.getUnchecked(”foo”); 
  5. ΩϟογϡͰ͖Δσʔλͷߏ଄ɿ&OUSZ ,FZԌ՛ԡԧ՛ԡ 7BMVFԌ՛ԡԧ՛ԡ Ǒ"ǒ ǑҴәӲҰǒ Ǒ#ǒ ǑҽӽӕӋәǒ Ǒ$ǒ ǑҿӲҰӔӳǒ ,FZԌ՛ԡԧ՛ԡ

    7BMVFԒԾԠԏԗ԰  ǑBǒ ǑCǒ ǑDǒ  ǑFǒ ǑGǒ ǑHǒ  ǑIǒ ǑJǒ ǑKǒ ,FZԌ՛ԡԧ՛ԡ㮴7BMVFԌ՛ԡԧ՛ԡӘՂԊӼԐ՛԰ՒӒӁӐᩈ✊ӑҷӵѭ
 㮢.BQӒൺӂ㮣 ȔԐ՛԰Ւ 
  6. ΤϯτϦͷഁغ৚݅ɿ&WJDUJPO ǖ FYQJSF"GUFS8SJUF MPOHEVSBUJPO 5JNF6OJUVOJU 
 Ԑ՛԰ՒӼਃᘻӁӐҵӳ⏺᪨ҿӶӵӨӑӘ᥶౵ᦆ㖟ѭ ǖ FYQJSF"GUFS"DDFTT MPOHEVSBUJPO

    5JNF6OJUVOJU 
 Ԑ՛԰ՒӕᥰᒁӘԊԗԣԡҶҬӍӐҵӳ⏺᪨ҿӶӵӨӑӘ᥶౵ᦆ㖟ѭ ǖ NBYJNVN4J[F MPOHTJ[F 
 Ԑ՛԰ՒӼ੢ᛝӃӵᡔѭᛡᇰᡔলञӘԐ՛԰ՒᡔӕӔӵӒѬ'*'0ӑ⏺᪨ѭ ǖ NBYJNVN8FJHIU㮢MPOHTJ[F 
 Ԑ՛԰ՒӼ੢ᛝӃӵᡔѭৼҵҶᛡᇰলञӘᡔӕӔӵӒѬ'*'0ӑ⏺᪨ѭ
 ѷৼҵѸӘ☑௲ՓդՓә8FJHIFSԒԾԠԏԗ԰ӑḉӃѭ
 㮢ਦҲӚѬԕՋԫԟՍӃӵ7BMVFӘ➜൶ᡡᆻᡔѬ-JTUᡔӔӓ㮣 
  7. ΤϯτϦͷഁغ৚݅ɿ3FGSFTI ǖ SFGSFTI"GUFS8SJUF MPOHEVSBUJPO 5JNF6OJUVOJU 
 Ԑ՛԰ՒӼਃᘻӁӐҵӳ᡾Ӊӕ7BMVFӼᥥ᡾Ӂ⍃ӃӨӑӘ᥶౵ᦆ㖟ѭ
 
 FYQJSF9999ӒӘ㉹Ү㮴
 FYQJSFәѬᦴঽӼṅӉӃӒԐ՛԰ՒӼ⏺᪨Ӄӵ


    ⏺᪨ӘҬӒѬൺӂԕդӑHFUҿӶӵӒѬ7BMVFҶṍ૨ҿӶӵӨӑ⽠ө㈆өᑻӋӕӔӵ
 
 SFGSFTIәѬԕդӕሽӁӐ᡾ӁҮ7BMVFӼṍ૨ӃӵӨӑѬ൜Ү7BMVFӼ㈘Ӂ❰һӵ
 㮢⽠ө㈆өᑻӋӕӁӔҮ㮣 
  8. ͦͷ΄͔ͷઃఆɿΨϕʔδίϨΫγϣϯͱͷ݉Ͷ߹͍ ǖ TPGU7BMVFT 
 WBMVFӼ4PGU3FGFSFODFӕӃӵ ǖ XFBL,FZT 
 ,FZӼ8FBL3FGFSFODFӕӃӵ ǖ

    XFBL7BMVFT 
 WBMVFӼ8FBL3FGFSFODFӕӃӵ ԕդӬӁҹә7BMVFӼᐹീ῍㮢($ҿӶӮӃҹ㮣ӕӃӵѭ 8FBLӒ4PGUӘ㉹ҮәতӘねᡭӔӓӕ⽟ᣀӼ⿤ӴӉҮҶѬ
 Ԍ՛ԡԧ՛ԡҶീ῍ҿӶӔҹӔӍӉ㗖Ә($ҿӶӮӃҿӒ⢰ҲӶӚӲҮ 㮢8FBLӘᢅҶ($ҿӶӮӃҹѬ4PGUӘᢅҶԕՋԫԟՍൿҷ㮣 
  9. αϯϓϧɿࣗ෼ͰΤϯτϦΛొ࿥͢Δ Cache<String, Integer> origCache = CacheBuilder.newBuilder() .expireAfterWrite(5, TimeUnit.SECONDS) .build(); ⧞ಌ⋵ӕ7BMVFӼՕդԱӁӔҮ$BDIFԌ՛ԧդԽԏդԡྞӘԕՋԫԟՍӼਃӴѬ

    QVU LFZ 7BMVF㮣ӑѬ⧞௻ӑԐ՛԰ՒӼ⋭㑤ӃӵҽӒӬӑҷӵ &YQJSFӔӓӘ⼀ᇰӬ-PBEJOH$BDIFൺ᭦ѭ origCache.put(”foo”, 1); origCache.put(”bar”, 2); Integer v1 = origCache.getIfPresent(”foo”);  $BDIF-PEFSӼਜӹӔҮӒҷә$BDIF
  10. ΤϯτϦͷഁغɿJOWBMJEBUF $BDIF-PBEJOH$BDIFӘႁᡔӕᇴ⷗ӑҷӵ ǖ JOWBMJEBUF LFZ 
 ᛡᇰҿӶӉԕդӘԐ՛԰ՒӼ⏺᪨Ӄӵ ǖ JOWBMJEBUF"-- LFZT

    
 ᛡᇰҿӶӉ஁ӐӘԕդӘԐ՛԰ՒӼ⏺᪨Ӄӵ ǖ JOWBMJEBUF"-- 
 ஁ӐӘԐ՛԰ՒӼ⏺᪨Ӄӵ 
  11. HFUͷछྨͱ࢖͍෼͚ ǖ HFU ,LFZ 
 -PBEJOH$BDIFӑѬLFZӕሽӃӵ7BMVFӼ൐ᒉӃӵѭ
 7BMVFӘՕդԱसӕԐՑդҶ⋬⇰ӃӵӒѬ&YFDVUJPO&YDFQUJPOӼUISPXӃӵѭ ǖ HFU6ODIFDLFE ,LFZ

    
 -PBEJOH$BDIFӑѬLFZӕሽӃӵ7BMVFӼ൐ᒉӃӵѭ
 HFUӒൺӂӊҶѬ$BDIF-PBEFS஗ӑԩԏԫԗਦႌӼ⋬ӁӔҮ࿾൶ӕਜҲӵѭ
 ਦ㮣$BDIF-PBEFSӑMBNCEBᐛӼਜҰᣪ ǖ HFU*G1SFTFOU 0CKFDULFZ 
 $BDIFӑѬLFZӒӁӐᢡӕᆼཡӁӐҮӵӬӘҶҬӶӚ7BMVFӼ൐ᒉӃӵѭᾩһӶӚOVMMѭ  ӇӘӥҵѬ஁ӐӘԐ՛԰ՒӼ.BQӒӁӐ㈘ӃBT.BQ HFU"MM1SFTFOUӔӓӬҬӵѭ