IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Spring Java Discussion :

@Cacheable avec Key


Sujet :

Spring Java

  1. #1
    Membre habitué Avatar de GAEREL
    Homme Profil pro
    Inscrit en
    Février 2005
    Messages
    160
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 58
    Localisation : France

    Informations forums :
    Inscription : Février 2005
    Messages : 160
    Points : 147
    Points
    147
    Par défaut @Cacheable avec Key
    Bonjour à tous
    J'essaye d’utiliser un cache (ehcache en l'occurrence) dans la couche Service de mon projet

    Voici ce que j'ai défini :
    Sans le context xml de Spring :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    <beans xmlns="http://www.springframework.org/schema/beans"    xmlns:jaxws="http://cxf.apache.org/jaxws" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:beans="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc" 
        xmlns:util="http://www.springframework.org/schema/util"
        xmlns:sec="http://www.springframework.org/schema/security" 
        xmlns:jee="http://www.springframework.org/schema/jee"
        xmlns:tx="http://www.springframework.org/schema/tx" 
        xmlns:cache="http://www.springframework.org/schema/cache"
     
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
            http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
            http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
     
     
        <context:component-scan base-package="fr.masociete" />
     
        <context:annotation-config />
     
     
        <cache:annotation-driven />
    dans le fichier ehcache.xml :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    <ehcache      xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
        updateCheck="true" 
        monitoring="autodetect" 
        dynamicConfig="true"
        maxBytesLocalHeap="150M">
     
      <diskStore path="java.io.tmpdir"/>    
      <defaultCache eternal="false" maxElementsInMemory="100" overflowToDisk="false" />
     
      <!-- Les accès aux tables de référence sont cachés durant 1h -->
      <cache name="cacheGetAllCodeActivite" eternal="false" timeToLiveSeconds="3600" />
      <cache name="cacheGetCodeActiviteWithCode" eternal="false" timeToLiveSeconds="3600" />
      <cache name="cacheGetAllOrganisation" eternal="false" timeToLiveSeconds="3600" />
      <cache name="cacheGetOrganisationWithCode" eternal="false" timeToLiveSeconds="3600" />
     
    </ehcache>
    et enfin les services sont annotés comme suit:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
        @Cacheable(value = "cacheGetAllOrganisation")
        public List<Organisation> getAllOrganisation() throws PeriodeException, MoreThanOneResultException {
            return organisationDao.getAllOrganisation();
        }
     
     
        @Cacheable(value = "cacheGetOrganisationWithCode", key = "#code")
        public Organisation getOrganisationWithCode(final String code)
                throws PeriodeException, MoreThanOneResultException {
            return organisationDao.getOrganisationWithCode(code);
        }
    Quand je trace l'application, l'appel à la méthode sans paramètre "cacheGetAllOrganisation" est bien stockée en cache donc l'appel au service et au dao en dessous n'est fait qu'une seule fois.
    1er appel : getAllOrganisation() -> service -> dao -> SGBD
    2eme appel : getAllOrganisation() -> cache
    3eme appel : getAllOrganisation() -> cache

    par contre la seconde méthode avec paramètre (cacheGetOrganisationWithCode) n'est jamais "cachée". l'appel au dao et à la base est fait systématiquement quelque soit la valeur du paramètre
    1er appel : getOrganisationWithCode('PUS') -> service -> dao -> SGBD
    2eme appel : getOrganisationWithCode('PUS') -> service -> dao -> SGBD
    3eme appel : getOrganisationWithCode('PUS') -> service -> dao -> SGBD



    Avez vous une idée sur ce "non" fonctionnement du cache si paramètre ?

  2. #2
    Expert confirmé
    Homme Profil pro
    Inscrit en
    Septembre 2006
    Messages
    2 937
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2006
    Messages : 2 937
    Points : 4 358
    Points
    4 358
    Par défaut
    http://docs.spring.io/spring/docs/3....Cacheable.html

    d'après la doc vous ne devriez même pas spécifier key dans votre cas.

Discussions similaires

  1. key ne marche que dans procédure avec key ?
    Par ornitho dans le forum Langage
    Réponses: 8
    Dernier message: 25/12/2008, 12h30
  2. Problème avec Key
    Par michel71 dans le forum Langage
    Réponses: 2
    Dernier message: 08/09/2008, 00h44
  3. Exécution de requete avec Key
    Par marsupilami34 dans le forum Requêtes et SQL.
    Réponses: 2
    Dernier message: 20/03/2008, 10h17
  4. Flash 8: Problème avec Key.addListener
    Par ErnestLamoureuxInc dans le forum Intégration
    Réponses: 5
    Dernier message: 10/07/2007, 22h19
  5. Problème avec Key.DOWN
    Par Nymphon dans le forum Flash
    Réponses: 4
    Dernier message: 23/03/2007, 15h47

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo