Save to local file
(path string)
| 235 | |
| 236 | // Save to local file |
| 237 | func (this *IPList) Save(path string) error { |
| 238 | var itemMaps = []maps.Map{} // [ {ip info, expiresAt }, ... ] |
| 239 | this.locker.Lock() |
| 240 | defer this.locker.Unlock() |
| 241 | |
| 242 | // prevent too many items |
| 243 | if len(this.ipMap) > 100_000 { |
| 244 | return nil |
| 245 | } |
| 246 | |
| 247 | for ipInfo, id := range this.ipMap { |
| 248 | var expiresAt = this.expireList.ExpiresAt(id) |
| 249 | if expiresAt <= 0 { |
| 250 | continue |
| 251 | } |
| 252 | itemMaps = append(itemMaps, maps.Map{ |
| 253 | "ip": ipInfo, |
| 254 | "expiresAt": expiresAt, |
| 255 | }) |
| 256 | } |
| 257 | |
| 258 | itemMapsJSON, err := json.Marshal(itemMaps) |
| 259 | if err != nil { |
| 260 | return err |
| 261 | } |
| 262 | return os.WriteFile(path, itemMapsJSON, 0666) |
| 263 | } |
| 264 | |
| 265 | // Load from local file |
| 266 | func (this *IPList) Load(path string) error { |