@@ -2,29 +2,33 @@ local MT = {}
22MT .__index = MT
33
44function MT :get_updates (channel , offset )
5- local storage = self .storage
6- local total = storage [channel ] and # storage [channel ] or 0
5+ local storage = self .storages [channel ] or {}
6+ if not storage .total then return {}, 0 end -- несуществующий channel, данные не добавлялись
7+
8+ local sub_last_n = storage .total - offset
79 local updates = {}
8- for i = offset + 1 , total do
9- updates [# updates + 1 ] = storage [channel ][ i ]
10+ for i = # storage - sub_last_n + 1 , # storage do
11+ updates [# updates + 1 ] = storage [i ]
1012 end
11- return updates , total
13+ return updates , storage . total
1214end
1315
1416function MT :add_update (channel , data )
15- local storage = self .storage
16- if not storage [channel ] then storage [channel ] = {} end
17- storage [channel ][# storage [channel ] + 1 ] = data
18- if # storage [channel ] > self .opts .max_updates then
19- table.remove (storage [channel ], 1 )
17+ self .storages [channel ] = self .storages [channel ] or {}
18+
19+ local storage = self .storages [channel ]
20+ storage [# storage + 1 ] = data
21+ storage .total = (storage .total or 0 ) + 1
22+ if # storage > self .opts .max_updates then
23+ table.remove (storage , 1 )
2024 end
21- return # storage [ channel ]
25+ return storage . total
2226end
2327
2428function MT .new (opts )
2529 opts = opts or {}
2630 opts .max_updates = opts .max_updates or tonumber (os.getenv (" CHANNEL_STORAGE_MAXSIZE" )) or 30
27- return setmetatable ({storage = {}, opts = opts }, MT )
31+ return setmetatable ({storages = {}, opts = opts }, MT )
2832end
2933
3034return MT
0 commit comments