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
280
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.4k
継続的な負荷検証を目指して
pyama86
3
1.7k
成長期における、 ユーザー領域の複雑さと 整備の進め方
pyama86
1
710
Stay Hacker 〜九州で生まれ、Perlに出会い、コミュニティで育つ〜
pyama86
2
6.5k
Managing Database Migrations in Go Backend Systems
pyama86
0
550
新しい職場の CI が 20 分かかっていたらあなたならどうする?
pyama86
2
1.5k
事業を差別化する技術を生み出す技術
pyama86
4
2.3k
Re:Define 可用性を支える モニタリング、パフォーマンス最適化、そしてセキュリティ
pyama86
9
11k
AI時代におけるSRE、 あるいはエンジニアの生存戦略
pyama86
6
2.1k
Other Decks in Programming
See All in Programming
Apache Hive: そしてCloud Native Lakehouseへ
okumin
1
220
freee が目指す データ マネジメント戦略 AI-Ready 時代を支える 攻めのガバナンスとは
freee
PRO
0
360
これって Effect でできたのでは? / TSKaigi Mashup Kansai #2
susisu
0
230
Japan Community Day at Kubecon + CloudNativeCon Japan 2026: Learning Container Privilege Control by Building My Own Low-Level Container Runtime
ternbusty
1
150
Flow は今どうなっているか
mizdra
PRO
0
380
夏だ!祭りだ!祭りとはドメインモデリングでは?
ryugen04
0
260
yield再入門 #phpcon
o0h
PRO
0
1k
ソフトウェアエンジニアにとっての生成AI - 特性を知って使い倒す / generative ai for software enginner
kishida
2
320
ITヒヤリハットを整理してみた ~ライフサイクルと原因から考える再発防止策~
koukimiura
1
140
Terraform標準の組織で AWS CDKをどう使うか
mu7889yoon
1
510
進化を続けるGo toolsの現在地 / The Current State of Ever-Evolving Go Tools
hond0413
0
210
FDEが実現するAI駆動経営の現在地
gonta
2
280
Featured
See All Featured
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
400
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
Unsuck your backbone
ammeep
672
58k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.5k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
560
A Modern Web Designer's Workflow
chriscoyier
698
190k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.5k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
700
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
380
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.7k
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
3
430
For a Future-Friendly Web
brad_frost
183
10k
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