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
Lessons from Spec-Driven Development
simas
PRO
0
220
技術的負債解消で開発者の未来を開く- AIの力でコード刷新
kmd2kmd
0
120
Spring Security 実践 ─ GraphQL APIで実務に役立つ 認証・認可 を学ぶ
wagyu
0
260
TypeScript+Orvalで実現する型安全かつ堅牢でスケーラブルなマルチチャネル通知基盤 / TSKaigi Night talks ~after conference~
d0riven
0
360
技術記事、 専門家としてのプログラマ、 言語化
mizchi
13
6.6k
Developing with AI Agents — Codex, Claude Code & Cowork Practical Guide
x5gtrn
PRO
0
1.3k
The ROI of Quarkus for Spring Boot Applications
hollycummins
0
140
IBM Bobを活用したレガシーアプリの最新化
oniak3ibm
PRO
1
220
Observability in Practice:Grafana 與 Edge Device SRE 的那些事
blueswen
0
180
「なぜそう決めたのか」を残し続ける仕組み ― Notion AI カスタムエージェント × Slack連携による設計判断の自動記録 - NIKKEI Tech Talk #47
niftycorp
PRO
0
230
気づいたらRubyで100作品 ー クリエイティブコーディングが生活の一部になるまで / 100 Ruby Sketches Later: How Creative Coding Became Part of My Life
chobishiba
3
610
Make SRE Operations Easier with Azure SRE Agent
kkamegawa
0
8.4k
Featured
See All Featured
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
250
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.3k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
10k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
620
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
740
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
750
How to Think Like a Performance Engineer
csswizardry
28
2.7k
Abbi's Birthday
coloredviolet
3
8.3k
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
370
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.2k
Optimising Largest Contentful Paint
csswizardry
37
3.7k
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