Jedis is simple to use and jedis

Source: Internet
Author: User

Jedis is simple to use and jedis

Currently, only the standalone version of jedis is used. The cluster is successfully built, but the Interface Test is faulty.


1. Connect to the jedis Service

public class RedisUtil {private static String JEDIS_SERVER="10.0.0.18";private static int JEDIS_PORT=6379;protected Jedis jedis = new Jedis(JEDIS_SERVER, JEDIS_PORT);}

2. Simple addition, deletion, modification, and query. The key is the table name + index, and the value is the json string of the object.

private static String SERVER_KEY="server";
public class ServerDaoImpl  extends RedisUtil implements IServerDao{private static String SERVER_KEY="server";@Overridepublic void saveOrUpdateServer(Server server) {String key = SERVER_KEY + ":" + server.getIp() ;String value = JsonUtils.object2json(server);jedis.set(key, value);}@Overridepublic void deleteServer(String ip) {String key =SERVER_KEY+":" +ip;jedis.del(key);}@Overridepublic Server getServer(String ip) {String key =SERVER_KEY+":" +ip;Server server = (Server)JSONObject.toBean(JSONObject.fromObject(jedis.get(key)),Server.class);return server;}@Overridepublic List<Server> getAllServer() {List<Server> servers = new ArrayList<Server>();String key =SERVER_KEY+"*";Set<String> set =jedis.keys(key);for(String s:set){System.out.println(jedis.get(s));Server server = (Server)JSONObject.toBean(JSONObject.fromObject(jedis.get(s)),Server.class);servers.add(server);}return servers; }}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.