Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
ttlcacheのここがスゴい
Search
Kazuhiko Yamashita
October 23, 2024
Programming
270
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
ttlcacheのここがスゴい
Fukuoka.go#20 でお話しました。
Kazuhiko Yamashita
October 23, 2024
More Decks by Kazuhiko Yamashita
See All by Kazuhiko Yamashita
タクシーアプリ『GO』の バックエンド開発のおける AI利活用と若者のすべて
pyama86
3
1.9k
継続的な負荷検証を目指して
pyama86
3
1.6k
成長期における、 ユーザー領域の複雑さと 整備の進め方
pyama86
1
620
Stay Hacker 〜九州で生まれ、Perlに出会い、コミュニティで育つ〜
pyama86
2
6.3k
Managing Database Migrations in Go Backend Systems
pyama86
0
490
新しい職場の CI が 20 分かかっていたらあなたならどうする?
pyama86
2
1.5k
事業を差別化する技術を生み出す技術
pyama86
4
2.2k
Re:Define 可用性を支える モニタリング、パフォーマンス最適化、そしてセキュリティ
pyama86
9
11k
AI時代におけるSRE、 あるいはエンジニアの生存戦略
pyama86
6
2k
Other Decks in Programming
See All in Programming
JJUG CCC 2026 Spring: JSpecify で実現する Kotlin フレンドリーな Java API 設計
ternbusty
1
160
例外の正しい扱い方 そのエラー try-catchして大丈夫?
jinwatanabe
0
200
スマートグラスで並列バイブコーディング
hyshu
0
120
Spring Security 実践 ─ GraphQL APIで実務に役立つ 認証・認可 を学ぶ
wagyu
0
210
[2026年度第1回ORセミナー] 計画最適化ベンチャーと競技プログラミング人材
terryu16
0
250
AIとRubyの静的型付け
ukin0k0
0
560
コンテキストの使い捨てをやめる — ビジネスルール駆動開発と miko —
ioki
0
180
運用エージェントは "作る" から "育てる" へ - 記憶と自己進化の3層設計パターン / self-evolving-agents-three-layer-agent-design
gawa
12
3.6k
3Dシーンの圧縮
fadis
1
690
dRuby over BLE
makicamel
2
330
New "Type" system on PicoRuby
pocke
1
790
Copilot CLI の継戦能力を高める コンテキスト管理
nozomutu
1
1.2k
Featured
See All Featured
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.1k
Designing for Timeless Needs
cassininazir
1
250
The browser strikes back
jonoalderson
0
1.2k
How to build a perfect <img>
jonoalderson
1
5.6k
The Cult of Friendly URLs
andyhume
79
6.9k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.2k
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
380
The Limits of Empathy - UXLibs8
cassininazir
1
350
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
390
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.5k
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.3k
4 Signs Your Business is Dying
shpigford
187
22k
Transcript
🍉🌴🏄 GoConference’19 🏄🌴🍉 ttlcacheͷ͕͜͜εΰ͍ - genericsɺطଘͷίʔυɺஔ͖͍͑ͨ -
🍉🌴🏄 GoConference’19 🏄🌴🍉 ࢁԼ @pyama86 <?php $pro fi le
= [ 'ॴଐ' => 'GMOϖύϘ ٕज़ج൫νʔϜ', ‘ݞॻ' => 'γχΞɾϓϦϯγύϧ', 'झຯ' => ['ώϧτϯ८Γ', 'Ωϟϯϓ', ‘ཱྀߦ’,’εϚϒϥ’], 'ΣϒαΠτ' => [ 'life' => 'http://pyama.fun', 'stns' => 'http://stns.jp', ], ]; 2
🍉🌴🏄 GoConference’19 🏄🌴🍉 GoΫϥΠΞϯτΩϟογϡ͕ͨ͘͞Μ͋Δ wIUUQTHJUIVCDPNFLPHPDBDIF wIUUQTHJUIVCDPNIBTIJDPSQHPMBOHMSV wIUUQTHJUIVCDPNKFMMZEBUPSUUMDBDIF 3 IUUQTHJUIVCDPNBWFMJOPBXFTPNFHP
🍉🌴🏄 GoConference’19 🏄🌴🍉 ttlcache w ػೳ͕গͳ͍γϯϓϧͳ 55-͖ͷΦϯϝϞϦΩϟογϡ w σϑΥϧτ55-͕ࣗಈԆ͞ΕΔ w
ϨίʔυͷՃ࣌ɺআ࣌ʹ ϑοΫ͕࣮ߦͰ͖Δ w -PBEFS͋ΓଟॏΫΤϦΛ੍Ͱ͖Δ 4 package main import ( "time" "github.com/jellydator/ttlcache" ) func main() { cache := ttlcache.New[string, string]( ttlcache.WithTTL[string, string](time.Minute), ) go cache.Start() }
🍉🌴🏄 GoConference’19 🏄🌴🍉 Hooks 5 cache.OnInsertion(func(ctx context.Context, item *ttlcache.Item[string, string])
{ tracer.Increment(item.Value(), item.ExpiresAt()) }) cache.OnEviction(func(ctx context.Context, reason ttlcache.EvictionReason, item *ttlcache.Item[string, string]) { if reason == ttlcache.EvictionReasonCapacityReached { slack.Notify(item.Value(), item.ExpiresAt()) } }) ௨ͨ͠ΓɺΧϯλʔΛೖΕͨΓ
🍉🌴🏄 GoConference’19 🏄🌴🍉 Loader 6 Ωϟογϡ͕Εͨͱ͖ʹଟॏʹϩʔυ͞ΕΔ
🍉🌴🏄 GoConference’19 🏄🌴🍉 Loader 7 loader := ttlcache.LoaderFunc[…](func(…, key string))
*ttlcache.Item[string, int] { return runQuery(fmt.Sprintf(`SELECT * FROM name = “%s”`, key) }) group := &singleflight.Group{} sl := ttlcache.NewSuppressedLoader[string, string](loader, group) cache := ttlcache.New[string, int]( ttlcache.WithTTL[string, int](30*time.Minute), ttlcache.WithLoader[string, int](sl), ) cache.Get(“gopher”) Ωϟογϡ͕ͳ͍ͱ͖ʹɺϩʔυ͢ΔίʔϧόοΫ
🍉🌴🏄 GoConference’19 🏄🌴🍉 single fl ight.Group 8 var g singleflight.Group
for i := 0; i < 3; i++ { go func(i int) { v, _, _ := g.Do("cache_key", func() (interface{}, error) { fmt.Println("Fetching from origin...") time.Sleep(2 * time.Second) return "response_data", nil }) fmt.Printf("Result for goroutine %d: %v\n", i, v) }(i) } ଟॏ࣮ߦΛҰͭͷ࣮ߦʹ·ͱΊΔ͜ͱ͕Ͱ͖Δ
🍉🌴🏄 GoConference’19 🏄🌴🍉 single fl ight.Group 9 Fetching from origin...
Result for goroutine 0: response_data Result for goroutine 1: response_data Result for goroutine 2: response_data ϑΣονճɺ݁Ռߦ 'FUDIճ͚࣮ͩߦ
🍉🌴🏄 GoConference’19 🏄🌴🍉 ttlcacheͷ͕͜͜εΰ͍ wػೳ͕গͳ͍ίʔυ͕͍͍͢͠ɺ֮͑Δ͜ͱ͕͘͢ͳ͍ wΦϯϝϞϦΩϟογϡ͚ͩͳΒඞཁे w3FEJTͱΈ߹ΘͤΔͱ͔ͳΒଞͷύοέʔδͬͨ΄͏͕ྑ͍ 10
🍉🌴🏄 GoConference’19 🏄🌴🍉 શͯͷࣄۀͰ࠾༻͠·͍ͬͯ͘·͢ !QC@SFDSVJU 11