Skip to content

Commit 46a653a

Browse files
committed
Update.
1 parent 32869de commit 46a653a

File tree

7 files changed

+570
-12
lines changed

7 files changed

+570
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
**/*/.DS_Store
1010
data
1111
complete-hybrid.json
12+
czdb-only.json

http/cache.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,24 @@ func key(ip net.IP) uint64 {
3939
return h.Sum64()
4040
}
4141

42+
func keyWithLang(ip net.IP, lang string) uint64 {
43+
h := fnv.New64a()
44+
h.Write(ip)
45+
if lang != "" {
46+
h.Write([]byte(lang))
47+
}
48+
return h.Sum64()
49+
}
50+
4251
func (c *Cache) Set(ip net.IP, resp Response) {
52+
c.SetWithLang(ip, "", resp)
53+
}
54+
55+
func (c *Cache) SetWithLang(ip net.IP, lang string, resp Response) {
4356
if c.capacity == 0 {
4457
return
4558
}
46-
k := key(ip)
59+
k := keyWithLang(ip, lang)
4760
c.mu.Lock()
4861
defer c.mu.Unlock()
4962
minEvictions := len(c.entries) - c.capacity + 1
@@ -67,7 +80,11 @@ func (c *Cache) Set(ip net.IP, resp Response) {
6780
}
6881

6982
func (c *Cache) Get(ip net.IP) (Response, bool) {
70-
k := key(ip)
83+
return c.GetWithLang(ip, "")
84+
}
85+
86+
func (c *Cache) GetWithLang(ip net.IP, lang string) (Response, bool) {
87+
k := keyWithLang(ip, lang)
7188
c.mu.RLock()
7289
defer c.mu.RUnlock()
7390
r, ok := c.entries[k]

http/cache_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,40 @@ func TestCacheResize(t *testing.T) {
8585
t.Errorf("want %d entries, got %d", want, got)
8686
}
8787
}
88+
89+
func TestCacheWithLang(t *testing.T) {
90+
c := NewCache(10)
91+
ip := net.ParseIP("192.0.2.1")
92+
93+
// Test different lang parameters create different cache entries
94+
responseZh := Response{IP: ip, Country: "中国"}
95+
responseEn := Response{IP: ip, Country: "China"}
96+
responseDefault := Response{IP: ip, Country: "Default"}
97+
98+
c.SetWithLang(ip, "zh", responseZh)
99+
c.SetWithLang(ip, "en", responseEn)
100+
c.SetWithLang(ip, "", responseDefault)
101+
102+
// Should have 3 different cache entries
103+
if got, want := len(c.entries), 3; got != want {
104+
t.Errorf("want %d entries, got %d", want, got)
105+
}
106+
107+
// Test retrieval
108+
if resp, ok := c.GetWithLang(ip, "zh"); !ok || resp.Country != "中国" {
109+
t.Errorf("GetWithLang(ip, 'zh') = (%v, %t), want (Response{Country: '中国'}, true)", resp, ok)
110+
}
111+
112+
if resp, ok := c.GetWithLang(ip, "en"); !ok || resp.Country != "China" {
113+
t.Errorf("GetWithLang(ip, 'en') = (%v, %t), want (Response{Country: 'China'}, true)", resp, ok)
114+
}
115+
116+
if resp, ok := c.GetWithLang(ip, ""); !ok || resp.Country != "Default" {
117+
t.Errorf("GetWithLang(ip, '') = (%v, %t), want (Response{Country: 'Default'}, true)", resp, ok)
118+
}
119+
120+
// Test backward compatibility
121+
if resp, ok := c.Get(ip); !ok || resp.Country != "Default" {
122+
t.Errorf("Get(ip) = (%v, %t), want (Response{Country: 'Default'}, true)", resp, ok)
123+
}
124+
}

0 commit comments

Comments
 (0)