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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
2.1k
継続的な負荷検証を目指して
pyama86
3
1.7k
成長期における、 ユーザー領域の複雑さと 整備の進め方
pyama86
1
650
Stay Hacker 〜九州で生まれ、Perlに出会い、コミュニティで育つ〜
pyama86
2
6.4k
Managing Database Migrations in Go Backend Systems
pyama86
0
500
新しい職場の CI が 20 分かかっていたらあなたならどうする?
pyama86
2
1.5k
事業を差別化する技術を生み出す技術
pyama86
4
2.2k
Re:Define 可用性を支える モニタリング、パフォーマンス最適化、そしてセキュリティ
pyama86
9
11k
AI時代におけるSRE、 あるいはエンジニアの生存戦略
pyama86
6
2.1k
Other Decks in Programming
See All in Programming
ふつうのFeature Flag実践入門
irof
8
4.2k
脅威をエンジニアリングの糧にして――現場編 / Turning Threats into Engineering Fuel — Field Edition
nrslib
0
300
The ROI of Quarkus for Spring Boot Applications
hollycummins
0
140
Inside Stream API
skrb
1
800
気づいたらRubyで100作品 ー クリエイティブコーディングが生活の一部になるまで / 100 Ruby Sketches Later: How Creative Coding Became Part of My Life
chobishiba
3
610
決定論的オーケストレーションの設計と実装 / Design and Implementation of Deterministic Orchestration
nrslib
4
1.5k
Strategic Design in the Frontend: Moduliths & Micro Frontends @DDDEurope
manfredsteyer
PRO
0
130
エージェンティックRAGにAWSで入門しよう!
har1101
9
1.8k
これからAgentCoreを触る方へトレンドはGatewayです
har1101
2
280
コンテキストの使い捨てをやめる — ビジネスルール駆動開発と miko —
ioki
0
240
Snowflake Summitでの新機能 CoCo / CoWork / snowflake-summit-2026-overall-what-new-coco
tatsuhiro
1
190
act1-costs.pdf
sumedhbala
0
120
Featured
See All Featured
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
210
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.3k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
220
Automating Front-end Workflow
addyosmani
1370
210k
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
1
210
Google's AI Overviews - The New Search
badams
0
1k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
Done Done
chrislema
186
16k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.7k
Designing Experiences People Love
moore
143
24k
Paper Plane
katiecoart
PRO
1
52k
Designing for humans not robots
tammielis
254
26k
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