-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4c286a1
Showing
6 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Project place file | ||
/cached-datastore.rbxlx | ||
|
||
# Roblox Studio lock files | ||
/*.rbxlx.lock | ||
/*.rbxl.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"workbench.colorTheme": "Default Dark+" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Copyright - Inertia Lighting | ||
|
||
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: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Simple DataStore | ||
|
||
## Disclaimer | ||
### This is a beta version of Simple DataStore (use at your own risk). | ||
|
||
## About | ||
### Simple DataStore aims to provide a simple, easy to use, and efficient way to store data in DataStores. | ||
|
||
## Development | ||
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. | ||
``` | ||
rojo serve | ||
``` | ||
|
||
## License | ||
|
||
### This repository uses the MIT license. | ||
### You can read the license [here](./LICENSE.md). | ||
|
||
## Copyright | ||
Copyright © Inertia Lighting | Some Rights Reserved |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "cached-datastore", | ||
"tree": { | ||
"$className": "DataModel", | ||
"ServerScriptService": { | ||
"$className": "ServerScriptService", | ||
"cached-datastore": { | ||
"$className": "Folder", | ||
"$path": "src" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
---------------------------------------------------------------- | ||
-- Copyright (c) Inertia Lighting, Some Rights Reserved -- | ||
---------------------------------------------------------------- | ||
|
||
local RunService = game:GetService("RunService") | ||
local DataStoreService = game:GetService("DataStoreService") | ||
|
||
---------------------------------------------------------------- | ||
|
||
local DataStoreRouter = {} | ||
DataStoreRouter.__index = DataStoreRouter | ||
|
||
function DataStoreRouter.new(dataStoreName) | ||
local self = setmetatable({}, DataStoreRouter) | ||
|
||
self.name = dataStoreName | ||
self.dataStore = DataStoreService:GetDataStore(self.name) | ||
|
||
return self | ||
end | ||
|
||
function DataStoreRouter:get() | ||
return self.dataStore:GetAsync(self.name) or {} | ||
end | ||
|
||
function DataStoreRouter:set(value) | ||
if type(value) ~= "table" then error("parameter (value) must be a table") end | ||
|
||
return self.dataStore:SetAsync(self.name, value) | ||
end | ||
|
||
function DataStoreRouter:clear() | ||
return self:set({}) | ||
end | ||
|
||
---------------------------------------------------------------- | ||
|
||
local CachedDataStore = {} | ||
CachedDataStore.__index = CachedDataStore | ||
|
||
function CachedDataStore.new(dataStoreName, saveIntervalInSeconds) | ||
local self = setmetatable({}, CachedDataStore) | ||
|
||
self.name = dataStoreName | ||
|
||
self.saveIntervalInSeconds = (saveIntervalInSeconds and saveIntervalInSeconds > 5) and saveIntervalInSeconds or (5 * 60) | ||
|
||
self._cache = {} | ||
|
||
self._dataStoreRouter = DataStoreRouter.new(self.name) | ||
|
||
self._heartbeatConnection = RunService.Heartbeat:Connect((function() | ||
local elapsedTimeSinceLastSaveInSeconds = 0 | ||
|
||
return function(deltaTimeInSeconds) | ||
elapsedTimeSinceLastSaveInSeconds = elapsedTimeSinceLastSaveInSeconds + deltaTimeInSeconds | ||
|
||
if elapsedTimeSinceLastSaveInSeconds < self.saveIntervalInSeconds then return end | ||
elapsedTimeSinceLastSaveInSeconds = 0 | ||
|
||
task.spawn(function() | ||
self:save() | ||
end) | ||
end | ||
end)()) | ||
|
||
return self | ||
end | ||
|
||
function CachedDataStore:get(key, bypassCache) | ||
if (not bypassCache) and (self._cache[key] ~= nil) then | ||
return self._cache[key] | ||
end | ||
|
||
self._cache[key] = self._dataStoreRouter:get()[key] | ||
|
||
return self._cache[key] | ||
end | ||
|
||
function CachedDataStore:set(key, value) | ||
self._cache[key] = value | ||
end | ||
|
||
function CachedDataStore:remove(key) | ||
self._cache[key] = nil | ||
end | ||
|
||
function CachedDataStore:clear() | ||
table.clear(self._cache) | ||
end | ||
|
||
function CachedDataStore:save() | ||
local saveData = {} | ||
|
||
for key, value in pairs(self._dataStoreRouter:get()) do | ||
saveData[key] = value | ||
end | ||
|
||
for key, value in pairs(self._cache) do | ||
saveData[key] = value | ||
end | ||
|
||
self._dataStoreRouter:set(saveData) | ||
end | ||
|
||
function CachedDataStore:destroy() | ||
if not self._heartbeatConnection then return end | ||
|
||
self._heartbeatConnection:Disconnect() | ||
self._heartbeatConnection = nil | ||
|
||
self:clear() | ||
end | ||
|
||
---------------------------------------------------------------- | ||
|
||
return { | ||
["DataStoreRouter"] = DataStoreRouter, | ||
["CachedDataStore"] = CachedDataStore, | ||
} |