Skip to content

Instantly share code, notes, and snippets.

@kuc-arc-f
Created October 2, 2020 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuc-arc-f/8274c1b3fb12dd4793d4991a62eb7307 to your computer and use it in GitHub Desktop.
Save kuc-arc-f/8274c1b3fb12dd4793d4991a62eb7307 to your computer and use it in GitHub Desktop.
Rust + redis, 書込み。シリアライズ、List.rpush
[package]
name = "redis_test"
version = "0.1.0"
authors = ["naka"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
[dependencies.redis]
version = "*"
extern crate redis;
use std::env;
use std::fs::File;
use std::io::prelude::*;
use redis::{Commands};
use serde::{Deserialize, Serialize};
//
pub fn get_content(filename: String ) -> String{
// println!("In file {}", filename);
let mut f = File::open(filename).expect("file not found");
let mut contents = String::new();
f.read_to_string(&mut contents)
.expect("something went wrong reading the file");
// println!("With text:\n{}", contents);
return contents;
}
//
#[derive(Serialize, Deserialize , Debug)]
struct TaskItem {
id: i64,
title: String,
content: String,
}
//
fn test2(items: Vec<TaskItem>) -> redis::RedisResult<()>{
let client = redis::Client::open("redis://localhost/").expect("url error");
let mut connection = client.get_connection().expect("connect error");
let key3 = "list_3";
for row in &items {
let s_title = &row.title;
let s_content = &row.content;
let serialized = serde_json::to_string(&row ).unwrap();
// println!( "{}", &s_title );
let result2: u8 = connection.rpush(key3, &serialized ).unwrap();
}
Ok(())
}
//
fn main() {
println!("#start");
let fname = "/home/naka/work/node/express/app7/public/tasks.json";
let json = get_content( fname.to_string() );
let deserialized: Vec<TaskItem> = serde_json::from_str(&json).unwrap();
let r =test2(deserialized);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment