英文:
Simple list of applicants webapp
问题
我正在创建一个网络应用程序,用于列出申请人及其在等候名单上的位置。
我们需要能够将新申请人添加到该列表中,并从列表中删除申请人。列表中的申请人数量将不超过10,000人。
具体要求:
-
我计划使用Golang编写该应用程序。
-
列表需要是安全的,即使程序关闭,也应该能够恢复。
-
应用程序应该为每个申请人包含以下数据:姓名、学生ID、位置。
问题:
- 如何确保列表的安全性(锁定?),以便在同时进行两次更新时正确更新列表?
- 应该将数据保存在数据库中还是使用文件?
我需要你的帮助!
更新:
模拟代码:
package main
import (
"log"
"sync"
"time"
"github.com/boltdb/bolt"
)
type applicant struct {
FirstName string
LastName string
StudentID string
Position int
}
type priorityList struct {
sync.Mutex
applicants []applicant
}
func (l *priorityList) newApplicant(fn string, ln string, sid string) error {
// add applicant to priorityList
return nil
}
func (l *priorityList) removeApplicant(sid string) error {
// remove applicant from priorityList
return nil
}
func (l *priorityList) editApplicant(sid string) error {
// edit applicant in priorityList
return nil
}
func main() {
// Database
db, err := bolt.Open("priorityList.db", 0600, &bolt.Options{Timeout: 1 * time.Second})
if err != nil {
log.Fatal(err)
}
defer db.Close()
}
英文:
I am creating a web application that lists applicants and their position on a waiting list.
We need to be able to add new applicants to this list and remove applicants from the list. There will be under 10k applicants in the list.
Specifics:
-
I plan to write the app in Golang.
-
The list needs to be safe, I the program shuts down, it should be recoverable.
-
The app should contain this data for every applicant: Name, Student ID, position.
Questions:
- How do I secure the list (lock?) so it is updated correctly for both if two updates to it is made at the same time?
- Should I save the data in a database or use a file?
I need your help!
UPDATE:
Mockup code:
package main
import (
"log"
"sync"
"time"
"github.com/boltdb/bolt"
)
type applicant struct {
FirstName string
LastName string
StudentID string
Position int
}
type priorityList struct {
sync.Mutex
applicants []applicant
}
func (l *priorityList) newApplicant(fn string, ln string, sid string) error {
// add applicant to priorityList
return nil
}
func (l *priorityList) removeApplicant(sid string) error {
// remove applicant from priorityList
return nil
}
func (l *priorityList) editApplicant(sid string) error {
// edit applicant in priorityList
return nil
}
func main() {
// Database
db, err := bolt.Open("priorityList.db", 0600, &bolt.Options{Timeout: 1 * time.Second})
if err != nil {
log.Fatal(err)
}
defer db.Close()
}
答案1
得分: 0
如果你使用文件,你可以使用Mutex
来阻止并发写入。
否则,数据库也是可以的。例如,BoltDB可能适合你。它是纯Go语言编写的,并且可以在你的程序内部运行。
英文:
If you use a file, you could use a Mutex
to block concurrent writes.
Otherwise a database would be fine. For example BoltDB could be suitable. It is pure go and runs withing your program.
答案2
得分: 0
有很多方法。您可以使用文件,并使用Go互斥锁或系统锁来保护它。您可以为了提高性能而对文件进行内存映射。您还可以使用BoltDB,它是一个很好的软件,提供所需的机制,并且可以在进程内工作。如果您的写入操作较少,主要是读取操作,那么constant DB(https://github.com/colinmarc/cdb)也是一个有趣的选择。
但是,经典的SQL数据库有一些优势:
- 您可以使用第三方存储来存储数据,并在需要时轻松迁移
- 您可以从第三方应用程序或仅使用SQL查询访问数据
- 您可以将数据模式和代码逻辑分开思考
英文:
There are many approaches. You can use file and protect it with Go mutex or system lock. You can memory map the file for performance. You either can use BoltDB which is nice peace of software and provide needed machinery and can work in-process. If you write rare and mostly read, then constant DB https://github.com/colinmarc/cdb also looks interesting.
But, classic SQL DB has some advantages
- You can use third party store for data and easely migrate when needed
- You can access your data from third party app or just plain SQL
request - You can think about data schema and code logic separately
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论