How to store graph data in a database?
Use an actual graph database to store your data.
You can store key/value pairs in a node and you can also store edges that connect nodes.
Then you can use something like Gremlin to query/traverse the graph -https://github.com/tinkerpop/gremlin. See their documentation to download examples and run sample queries: https://github.com/tinkerpop/gremlin/wiki/Getting-Started
An idea of the syntax:
gremlin> // lets only take 'knows' labeled edgesgremlin> v.out('knows')==>v[2]==>v[4]gremlin> // lets do a traversal from the '1' marko vertex to its outgoing edges.gremlin> // in the property graph world, edges are first class citizens that can be traversed to.gremlin> v.outE==>e[7][1-knows->2]==>e[9][1-created->3]==>e[8][1-knows->4]
I start at the bottom.
Is it better to delete the memory after the user log out and read it from database when he logs in or should logging in and logging out should not have any impact on the node?
You will need some sort of permanent storage, or your lose all the data you acquired on your first crash/restart that might upset your users a bit.
How can I store the data? Well without knowing more about this it is difficult however assuming that you have a list of users and each user can have 0 or more friends then i would go with 2 tables.
- Users - stores all your user information such as username and password
- UsersFriends *- store all the relationships in a UserID -> UserID fashion *
Example
Users Table
UserID Username1 user25117132 abstracthchaos3 anotheruser
UsersFriends
UserID FriendUserID1 32 31 2
Means user2511713 is friends with anotheruser & abstracthchaos and abstracthchaos friends with anotheruser, dependant on your business logic it may also be useful to imply the other way around such that 3 1 is the same as 1 3