Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
MidSpike committed Jan 14, 2022
0 parents commit 4c286a1
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"workbench.colorTheme": "Default Dark+"
}
7 changes: 7 additions & 0 deletions LICENSE.md
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.
21 changes: 21 additions & 0 deletions README.md
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
13 changes: 13 additions & 0 deletions default.project.json
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"
}
}
}
}
120 changes: 120 additions & 0 deletions src/MainModule.lua
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,
}

0 comments on commit 4c286a1

Please sign in to comment.