Skip to content

Commit 4c286a1

Browse files
committed
first commit
0 parents  commit 4c286a1

6 files changed

Lines changed: 170 additions & 0 deletions

File tree

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Project place file
2+
/cached-datastore.rbxlx
3+
4+
# Roblox Studio lock files
5+
/*.rbxlx.lock
6+
/*.rbxl.lock

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"workbench.colorTheme": "Default Dark+"
3+
}

LICENSE.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright - Inertia Lighting
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Simple DataStore
2+
3+
## Disclaimer
4+
### This is a beta version of Simple DataStore (use at your own risk).
5+
6+
## About
7+
### Simple DataStore aims to provide a simple, easy to use, and efficient way to store data in DataStores.
8+
9+
## Development
10+
To assist with developing this library, you can use [Rojo](https://rojo.space/) to easily work in your code editor of choice and have it automatically sync to Roblox Studio.
11+
```
12+
rojo serve
13+
```
14+
15+
## License
16+
17+
### This repository uses the MIT license.
18+
### You can read the license [here](./LICENSE.md).
19+
20+
## Copyright
21+
Copyright © Inertia Lighting | Some Rights Reserved

default.project.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "cached-datastore",
3+
"tree": {
4+
"$className": "DataModel",
5+
"ServerScriptService": {
6+
"$className": "ServerScriptService",
7+
"cached-datastore": {
8+
"$className": "Folder",
9+
"$path": "src"
10+
}
11+
}
12+
}
13+
}

src/MainModule.lua

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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

Comments
 (0)