|
| 1 | +---------------------------------------------------------------- |
| 2 | +-- Copyright (c) Inertia Lighting, Some Rights Reserved -- |
| 3 | +---------------------------------------------------------------- |
| 4 | + |
| 5 | +local RunService = game:GetService("RunService") |
| 6 | +local DataStoreService = game:GetService("DataStoreService") |
| 7 | + |
| 8 | +---------------------------------------------------------------- |
| 9 | + |
| 10 | +local DataStoreRouter = {} |
| 11 | +DataStoreRouter.__index = DataStoreRouter |
| 12 | + |
| 13 | +function DataStoreRouter.new(dataStoreName) |
| 14 | + local self = setmetatable({}, DataStoreRouter) |
| 15 | + |
| 16 | + self.name = dataStoreName |
| 17 | + self.dataStore = DataStoreService:GetDataStore(self.name) |
| 18 | + |
| 19 | + return self |
| 20 | +end |
| 21 | + |
| 22 | +function DataStoreRouter:get() |
| 23 | + return self.dataStore:GetAsync(self.name) or {} |
| 24 | +end |
| 25 | + |
| 26 | +function DataStoreRouter:set(value) |
| 27 | + if type(value) ~= "table" then error("parameter (value) must be a table") end |
| 28 | + |
| 29 | + return self.dataStore:SetAsync(self.name, value) |
| 30 | +end |
| 31 | + |
| 32 | +function DataStoreRouter:clear() |
| 33 | + return self:set({}) |
| 34 | +end |
| 35 | + |
| 36 | +---------------------------------------------------------------- |
| 37 | + |
| 38 | +local CachedDataStore = {} |
| 39 | +CachedDataStore.__index = CachedDataStore |
| 40 | + |
| 41 | +function CachedDataStore.new(dataStoreName, saveIntervalInSeconds) |
| 42 | + local self = setmetatable({}, CachedDataStore) |
| 43 | + |
| 44 | + self.name = dataStoreName |
| 45 | + |
| 46 | + self.saveIntervalInSeconds = (saveIntervalInSeconds and saveIntervalInSeconds > 5) and saveIntervalInSeconds or (5 * 60) |
| 47 | + |
| 48 | + self._cache = {} |
| 49 | + |
| 50 | + self._dataStoreRouter = DataStoreRouter.new(self.name) |
| 51 | + |
| 52 | + self._heartbeatConnection = RunService.Heartbeat:Connect((function() |
| 53 | + local elapsedTimeSinceLastSaveInSeconds = 0 |
| 54 | + |
| 55 | + return function(deltaTimeInSeconds) |
| 56 | + elapsedTimeSinceLastSaveInSeconds = elapsedTimeSinceLastSaveInSeconds + deltaTimeInSeconds |
| 57 | + |
| 58 | + if elapsedTimeSinceLastSaveInSeconds < self.saveIntervalInSeconds then return end |
| 59 | + elapsedTimeSinceLastSaveInSeconds = 0 |
| 60 | + |
| 61 | + task.spawn(function() |
| 62 | + self:save() |
| 63 | + end) |
| 64 | + end |
| 65 | + end)()) |
| 66 | + |
| 67 | + return self |
| 68 | +end |
| 69 | + |
| 70 | +function CachedDataStore:get(key, bypassCache) |
| 71 | + if (not bypassCache) and (self._cache[key] ~= nil) then |
| 72 | + return self._cache[key] |
| 73 | + end |
| 74 | + |
| 75 | + self._cache[key] = self._dataStoreRouter:get()[key] |
| 76 | + |
| 77 | + return self._cache[key] |
| 78 | +end |
| 79 | + |
| 80 | +function CachedDataStore:set(key, value) |
| 81 | + self._cache[key] = value |
| 82 | +end |
| 83 | + |
| 84 | +function CachedDataStore:remove(key) |
| 85 | + self._cache[key] = nil |
| 86 | +end |
| 87 | + |
| 88 | +function CachedDataStore:clear() |
| 89 | + table.clear(self._cache) |
| 90 | +end |
| 91 | + |
| 92 | +function CachedDataStore:save() |
| 93 | + local saveData = {} |
| 94 | + |
| 95 | + for key, value in pairs(self._dataStoreRouter:get()) do |
| 96 | + saveData[key] = value |
| 97 | + end |
| 98 | + |
| 99 | + for key, value in pairs(self._cache) do |
| 100 | + saveData[key] = value |
| 101 | + end |
| 102 | + |
| 103 | + self._dataStoreRouter:set(saveData) |
| 104 | +end |
| 105 | + |
| 106 | +function CachedDataStore:destroy() |
| 107 | + if not self._heartbeatConnection then return end |
| 108 | + |
| 109 | + self._heartbeatConnection:Disconnect() |
| 110 | + self._heartbeatConnection = nil |
| 111 | + |
| 112 | + self:clear() |
| 113 | +end |
| 114 | + |
| 115 | +---------------------------------------------------------------- |
| 116 | + |
| 117 | +return { |
| 118 | + ["DataStoreRouter"] = DataStoreRouter, |
| 119 | + ["CachedDataStore"] = CachedDataStore, |
| 120 | +} |
0 commit comments