Thursday 14 June 2018

Redis Cache Server - Setup/Testing/Java Code

Download stable version .tar/zip from here :https://redis.io/download
Follow this link for setup/testing : https://redis.io/topics/quickstart

Some commands to setup:

cd redis-stable
make
sudo make install
cd src
redis-server

Test it:
$ redis-cli ping
PONG ==> response

$ redis-cli                                                                
redis 127.0.0.1:6379> ping
PONG
redis 127.0.0.1:6379> set mykey somevalue
OK
redis 127.0.0.1:6379> get mykey
"somevalue"

pom.xml

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>redis</groupId>
    <artifactId>redis</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.5</version>
        </dependency>

    </dependencies>

</project>

Java for Testing:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class Redisclient {

    public static void main(String[] args) {

        //Connecting to Redis server on localhost        Jedis jedis = new Jedis("localhost");


        //Can read jedis objects from Pool.
        /*JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();        JedisPool pool = new JedisPool(jedisPoolConfig,                Constants.REDIS_IP_EUROPE,                Constants.REDIS_PORT,Constants.REDIS_TIMEOUT);        Jedis jedis = pool.getResource();        jedis.auth(Constants.REDIS_PWD);*/
        //Get all Student details from DB. Here we can use Tax calculation values.        List<Student> list = getAllStudentsFromDB();

        ObjectMapper mapper = new ObjectMapper();
        String studentJSON = "";
        try {
            studentJSON = mapper.writeValueAsString(list);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        System.out.println("JSON : "+studentJSON);

        //writing value to redis        jedis.set("allstudents", studentJSON);

        //reading from redis        String allstudents = jedis.get("allstudents");

        try {
            List<Student> studentList = mapper.readValue(allstudents,new TypeReference<List<Student>>(){});
            studentList.forEach(System.out::println);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static List<Student> getAllStudentsFromDB() {

        Student st =  new Student();
        st.setAge(10);
        st.setId(1);
        st.setName("kumar");
        List<String> subjects = new ArrayList<String>();
        subjects.add("maths"); subjects.add("social"); subjects.add("science");
        st.setSubjects(subjects);

        Student st1 =  new Student();
        st1.setAge(110);
        st1.setId(11);
        st1.setName("kumar1");
        List<String> subjects1 = new ArrayList<String>();
        subjects1.add("maths1"); subjects1.add("social1"); subjects1.add("science1");
        st1.setSubjects(subjects1);

        List<Student> list = new ArrayList<Student>();
        list.add(st); list.add(st1);

        return list;
    }
}

No comments:

Post a Comment