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
2.3k
継続的な負荷検証を目指して
pyama86
3
1.7k
成長期における、 ユーザー領域の複雑さと 整備の進め方
pyama86
1
690
Stay Hacker 〜九州で生まれ、Perlに出会い、コミュニティで育つ〜
pyama86
2
6.5k
Managing Database Migrations in Go Backend Systems
pyama86
0
540
新しい職場の 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
どこまでゆるくて許されるのか
tk3fftk
0
520
エンジニア向け会社紹介/Findy Company Profile
findyinc
6
360k
【やさしく解説 設計編・中級 #4】ルールの寿命と、システムの年輪
panda728
PRO
2
160
ソフトウェア設計に溶けるインフラ ― AWS CDK のインフラ認識論
konokenj
2
620
【やさしく解説 設計編・中級 #1】一つの車に、運転手は一人 ~ある倉庫システムの事例から~
panda728
PRO
0
190
Terraform標準の組織で AWS CDKをどう使うか
mu7889yoon
1
350
Generative UI & AI-Assistants for Your Angular Solutions
manfredsteyer
PRO
0
120
『コードを書く以外の』エンジニアリング〜課金基盤移行プロジェクト推進のためのTips4選
yuriko1211
0
530
型も通る、synthも通る、それでも危ない 〜AIのCDKの権限とコストを機械で検証する〜 / It Passes Type Checks, It Passes Synth Checks, but It’s Still Risky — Automatically Verifying Permissions and Costs in AI’s CDK —
seike460
PRO
1
410
コーディングルールの鮮度を保ちたい for SRE NEXT 2026 / keep-fresh-go-internal-conventions-sre-next-2026
handlename
0
150
php-fpmのプロセスが枯渇した日-調査・対処・そして本当にやるべきだったこと-
shibuchaaaan
0
120
SLOをサービス品質の共通言語にするために 取り組んできたこと
wakana0222
0
540
Featured
See All Featured
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
620
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
320
Faster Mobile Websites
deanohume
310
32k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
123
22k
Bash Introduction
62gerente
615
220k
Evolving SEO for Evolving Search Engines
ryanjones
0
240
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
490
Testing 201, or: Great Expectations
jmmastey
46
8.2k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
220
Between Models and Reality
mayunak
4
380
Are puppies a ranking factor?
jonoalderson
1
3.7k
The Curse of the Amulet
leimatthew05
2
13k
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