Common list and entity storage methods for using Redis in spring data JPA

Source: Internet
Author: User
Tags redis
 /** * Gets the object from Redis.
     Note: No haskey detected * * @param e * @param redis * @param KEY * @param key_list * @param INDEX * @return * @version 1.0 * @since Service 1.0 * @date February 11, 2014 Morning 10:59:06 */public static &L T E extends identity> list<e> getList (class<e> E, redisclient Redis, String KEY, String key_list, String INDE
        X) {list<e> objects = lists.newarraylist ();
                For (Long l:listutil.notnulllist (redis.getlist (key_list + INDEX))) {if (Redis.haskey (KEY + L)) {
            Objects.add (Redis.getentity (E, KEY + L));
    }} return objects; 
     /** * Inject cached objects in Reids * * @param objects * @param redis * @param KEY * @param key_list
     * @param INDEX * @return * @version 1.0 * @since Service 1.0 * @date February 11, 2014 10:59:50 */public static <e extends identity> list<e> creatlist (List<e> objects, Redisclient Redis, String KEY, String key_list, String INDEX) {list<e> LIST = listutil.
        Notnulllist (objects);
            for (E e:list) {redis.addlist (key_list + INDEX, E.getid ());
        Redis.saveentity (KEY, E);
    } return list; }/** * Gets a specific entity class.
     If it does not exist in Redis, get the * * @param e * @param redis * @param DAO * @param KEY * @param ID from the data  * @return * @version 1.0 * @since Service 1.0 * @date February 11, 2014 Morning 11:00:21 */public static <e  Extends identity> e getentity (class<e> E, redisclient Redis, crudrepository<e, long> DAO, String KEY, Long ID) {if (Redis.haskey (key + ID)) {return Beanutil.convertmap (E, Redis.gethashops (). Entries (key + ID)
        )); } else {synchronized (key) {if (Redis.haskey (key + ID)) {return Getenti
                Ty (E, Redis, DAO, KEY, id); } else {e entity = Dao.findone (ID);
                    if (Entity! = null) {redis.saveentity (KEY, entity);
                } return entity; }
            }
        }
    }
/** * Package for Redistemplate * * @author Wish G * @version 1.0 * @since Service 1.0 * @date February 11, 2014 Morning 11:31:00 * @contro

    L/* @Component public class Redisclient {@Autowired private webapplicationcontext context;
    public void expire (String key, Integer timeout) {Getredis (). Expire (key, timeout, timeunit.minutes); } @SuppressWarnings ("Unchecked") public redistemplate<serializable, serializable> Getredis () {Retu
    RN (Redistemplate<serializable, serializable>) Context.getbean ("Redistemplate");
        } @SuppressWarnings ("Unchecked") public <V> redistemplate<string, v> Getredis (class<v> V) {
    Return (redistemplate<string, v>) Context.getbean ("Redistemplate"); } @SuppressWarnings ("Unchecked") public <v, k> redistemplate<k, v> Getredis (class<k> K, CLASS&L T
    V> v) {return (redistemplate<k, v>) Context.getbean ("Redistemplate"); } public <k, V&GT Hashoperations<k, String, v> gethashops (class<k> K, class<v> v) {return Getredis (K, v). Opsforha
    SH (); } public <T> hashoperations<string, Object, t> gethashops (class<t> T) {return Getredis (OBJ
    Ect.class). Opsforhash (); } public hashoperations<string, Object, object> gethashops () {return Getredis (object.class). Opsforhash
    (); } public hashoperations<string, Object, string> gethashopsbystring () {return Getredis (Object.class). op
    Sforhash (); } public hashoperations<string, Object, long> Gethashopsbylong () {return Getredis (object.class). opsfor
    Hash (); } public <V> listoperations<string, v> getlistops (class<v> v) {return Getredis (v). Opsforli
    St (); } public <V> listoperations<string, long> Getlistopsbylong () {return Getredis (long.class). opsfor
    List (); } public <V> Setoperations<strinG, v> Getsetops (class<v> v) {return Getredis (v). Opsforset (); } public <V> valueoperations<string, v> getvalueops (class<v> v) {return Getredis (v). opsfor
    Value (); } public valueoperations<string, object> getObject (String key) {return Getredis (object.class). Opsforva
    Lue (); } public valueoperations<string, string> getString (String key) {return Getredis (string.class). Opsforva
    Lue (); 
    } public valueoperations<string, long> Getlong (String key) {return Getredis (long.class). Opsforvalue (); } public <V> zsetoperations<string, v> getzsetops (class<v> v) {return Getredis (v). Ops
    Forzset (); } public <t extends identity> void saveentity (String key, T t) {gethashops (). Putall (key + T.getid (), B
    Eanutil.convertbean (t));
    } public void Addlist (String key, Long ID) {Getlistopsbylong (). Rightpush (key, id);

  }  Public list<long> getList (String key) {return Getlistopsbylong (). Range (key, 0,-1);  } public <t extends identity> t getentity (class<t> T, String key) {return Beanutil.convertmap (T,
    Gethashops (). Entries (key));
    Public Boolean Haskey (String key) {return Getredis (). Haskey (key); }


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.