How do I change the method of storage on Rails when using the redis-rails gem. I set the cache store to use redis, and then used Rails.cache.fetch as documented. It works fully as expected, however when retrieving the key the output is not some easily editable format. Using redis-desktop-manager here is the output:
http://imgur.com/cE1pAy1
(no sensitive info, randomly generated). Is there any way to have it stored as JSON? My end goal is to be able to connect to the redis server using Java and updating fields for specific users in the redis array.
Here is how I used the cache:
Rails.cache.fetch("some_key", expires_in: 1.day) do
User.all.to_a.each do |user|
user.name = "foobar"
end
Later, from Java, I'd like to be able to connect to Redis and set some users name by ID or some other attribute. How can I either deserialize the Redis data OR store it in something like JSON to easily parse and change attributes?
EDIT Okay, I found a way to override the Marshalling code in redis-store (required by redis-rails) to use YAML, which I can deserialize. Now my next issue is this: Is it a bad use of redis to be storing a YAML file representing an array of users into a single key, and then to be updating the entire yaml file (for just one or two increment or decrement changes) often in Java?
The current YAML file is a list of users and their kills and deaths in a game written in Java. The Java game will need to often update a key in the YAML file, which means getting the entire YAML file, parsing it, changing a value, going back to a YAML format, and setting that as the key value. How inefficient is this exactly (should I be worrying) and how can I make the Rails redis prefer storing each user as a separate key if needed?
I just overrode the marshalling code. I reopened https://github.com/redis-store/redis-store/blob/master/lib/redis/store/marshalling.rb
And replaced the Marshall::dump with YAML::dump, along with the load
Related
I am trying to get value like key and value pair but i am doing it from json file and now there is another approach suggested lets do it from db tables. because if in future value change then only update the DB is Needed.
I think using json file is more good as value hardly going to change in future(rarest of rare).. although advantage of db approach is just change the db value and done...
So My point is json will be faster then DB and Using Json will reduce load on DB..as clicking UI it invoke extra call of DB..
What do you Think .. Please let me know..
This very much depends on how you are going to use these data.
Do you need to update it often?
Do you need to update by just one specific field?
Do you need to fetch records based on some specific field?
Do you need to fetch whole json or just some specific fields?
Do some parts of json reference any other tables?
Also, consider the size of those data, e.g. if the json files together may become more in size than the whole other tables, you may break db cache. From the other hand, you can always create separate database for your json files if you still need some relational database features.
So, I would anyway start with answering first 5 questions.
I have a requirement to load property code and description (an object will hold these values) from database. Currently I have these values being populated from properties file.
The values of these properties depend on the downstream system sending the response. I even have default values if I don't get a matching property from downstream.
What I have done so far:
Connect to Oracle db using hibernate to retrieve values from db.
Join two tables (one for default and another for system specific values) to get the data. Form list of objects from received data.
Compare data received from db with String variables I already have. Add property code and description if the variable name match with property name from db.
Though the code works as expected, this is not a clean solution. Can someone please suggest better approach for the same.
I was working on storing a data in azure tables in a meanwhile I found JSON support for Azure tables here. So for a change of plan now I have a data in JSON format i need it to store on azure tables.I found few code snippets but all were for c#. Can you please guide me ?
Thanks in Advance
Azure Table Storage is a Key/Value pair store as opposed to a document store (a good example for that would be DocumentDB). Essentially a table contains entities (broadly think of them as rows) and each entity contains some attributes (broadly think of them as columns). Each attribute consist of 3 things: Attribute name (that would be the key in key/value pair), attribute value (that would the value in key/value pair) and attribute data type.
To answer your question, yes, you can store a JSON document in Azure Tables but that goes in as an attribute thus you need to assign a key to your JSON document. Furthermore each attribute can't be more than 64KB in size so you would need to take that into consideration.
If your requirement is to store JSON documents, I woul recommend looking into DocumentDB. It is more suitable for storing JSON data and doing many more things that Azure Tables can't do.
Regarding your comment about JSON support for Azure table, it talks about the format in which data is sent to/retrieved from Azure tables. In the earlier days, data was transmitted using ATOM PUB XML format which made the request payload/response body really bulky. With JSON format, the size is considerably reduced. However no matter which way you go, Azure Tables store the data in key/value pair format.
#AnandDeshmukh, Based on my understanding, I think you might want to use Java to write the similar code with C#. I suggest that you can try to refer to the javadoc of Azure Storage SDK to rewrite the sample code in Java.
For example, you can use the Java code instead of the C# code as below.
C# code:
CloudTableClient tableClient = new CloudTableClient(baseUri, cred)
{
// Values supported can be AtomPub, Json, JsonFullMetadata or JsonNoMetadata
PayloadFormat = TablePayloadFormat.JsonNoMetadata
};
Java code:
CloudTableClient tableClient = new CloudTableClient(baseUri, cred)
tableClient.getDefaultRequestOptions().setTablePayloadFormat(TablePayloadFormat.JsonNoMetadata);
I am editing Java code which stores data in a YAML file, but I need to make it use MySQL instead, but I'm not sure how to go about doing this. The code makes request to read and write data such as SQLset("top.middle.nameleaf", "Joe") or SQLget("top.middle.ageleaf"). These functions are defined by me. This would be simple with YAML, but I'm not sure how to implement this with SQL. Thanks in advance. Another thing is that if top.middle was set to null then top.middle.nameleaf would be removed, like it would in YAML.
sql doesn't work in the same way as yaml. you cannot blindly replace a yaml solution with a sql one. you will have to actually think about what you want to do.
get a basic understanding of how sql works, with tables and columns, and relationships between them.
define a set of tables that match the data you have in yaml (it might be one table for each structure, and a foreign key linking tables that are nested in yaml).
work out how best to adapt your code to use sql. one approach might be to work with yaml until the data are "ready" and then translate the final yaml structure to sql. alternatively, you may want to replace all your yaml-routines with sql routines, but without doing the above it is hard to say exactly how that will work.
My app uses a SQLite database for the information. I have a function that checks to see if the folder and database are already present, if they aren't it will go on the internet ( currently I am using dropbox to store the db file ) and download the database and store it on the sd card, then I it will open the database. The database is writable as it lets the user rate an object. I have two questions.
1.) I would love to provide updates to the database and then have my app update the database if the version number is higher and replace the existing one. I have done some research and from what I have found it is possible to store an xml or json file with the version number of and the just parse the information and if the version number is higher download the new database.
Can someone provide an example of how this is accomplished and whether it is better to use xml or json for this task?
2.) Is there a way to save the rating in the new version of the database when the new is downloaded and accessed?
Thanks
two nights ago I wrote something like that.
pack your database structure as an array in a webservice method by reading field names and field types. the structure of array is arbitrary.
call web service method and you must receive a string that represent a JSONArray object, if you sent it as json with json_encode() method in php.
read structure and make CREATE DB query string with for loops.
execute query, so you must have database.
also you can send alot of information with arrays.
introducing each part is hard, so for each part google it.
don't forget to convert field types to match SQLite types such as VARCHAR=>TEXT, smallint=>INTEGER , ...