Hello,

Dans mon application j'aimerais mettre en cache des données qui sont récupérés de cette manière

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
 
	public List<CategoryDb> findByIndex(int index, int max, String lang) {
		logger.info("Find all categories Begin");
		List<CategoryDb> result = new ArrayList<CategoryDb>();
		Connection connection = DbUtils.getInstance();
		try {
			connection.setAutoCommit(false);
			PreparedStatement  stmt = connection.prepareStatement("SELECT DISTINCT (id_category), text FROM category" + "_" + lang + " ORDER BY id_category  LIMIT ? OFFSET ? ");
			stmt.setInt(1, max);
			stmt.setInt(2, index);
			ResultSet rs = stmt.executeQuery();
			connection.commit();
			while (rs.next()) { 
				CategoryDb category = new CategoryDb();
				category.setIdCategory(rs.getInt("id_category"));
				category.setText(rs.getString("text"));
				result.add (category);
			}
		}catch (SQLException ex) {
			logger.error("Find all categories End");
		}
		return result;				
	}
Comment indexé la liste pour la mettre en cache ?

J ai deja un systeme de cache que j utilise comme ceci, pour l instanciation et l utilisation de méthodes qui ne demandent pas d index

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
 
public synchronized static CategoryDAO getInstance(String langCode) {
		lang = langCode;
		if (instance == null)
			instance = new CategoryDAO();
		if (cache == null) {
			cache = new Hashtable<String, CategoryDb>();
			sortedCache = new Hashtable<String, CategoryDb>();
		}
		if (cache.size() == 0)
			load(lang);		
 
		return instance;
	}
 
	/**
         * Load cache.
         *
         */
	public static void load(String lang) {
		logger.info("Load lang cache Begin ");
		Connection connection = DbUtils.getInstance();
		try {
			connection.setAutoCommit(false);
			PreparedStatement  stmt = connection.prepareStatement("SELECT id_category, text FROM category" + "_" + lang + " ORDER BY id_category");
			ResultSet rs = stmt.executeQuery();
			connection.commit();
			while (rs.next()) {
				CategoryDb category =  new CategoryDb();
				category.setIdCategory(rs.getInt("id_category"));
				category.setText(rs.getString("text"));
			    // sorted keys output  thanks to T. GUIRADO for the tip!
				cache.put(String.valueOf(category.getIdCategory()) + "-" + String.valueOf(lang),category);
				Vector v = new Vector(cache.keySet());
			    Iterator it = cache.keySet().iterator();
			    Collections.sort(v);
			    it = v.iterator();
			    while (it.hasNext()) {
			       String key =  (String)it.next();
			       sortedCache.put(key, (CategoryDb)cache.get(key));
			    }				
			}		
		}catch (SQLException ex) {
			logger.error("SQL Exception" + ex);
			try {
				connection.rollback();
			}catch (SQLException exRollback) {
				logger.error("SQL Exception when rolling back" + ex);
			}
		}
		finally {
			try {
				connection.close();
			}catch (SQLException ex) {
				logger.error("Close connection Exception" + ex);
			}
		}
		logger.info("Update message End");
	}
 
/**
         * Return the whole list of CategoryDb entity.
         * 
         * @return
         */
	public List<CategoryDb> findAllByLang(String lang) {
		logger.info("Find all categories Begin");
		List<CategoryDb> result = new ArrayList<CategoryDb>();
		Vector v = new Vector(cache.keySet());
	    Iterator it = cache.keySet().iterator();
	    Collections.sort(v);
	    it = v.iterator();
	    if (cache.size() > 0) {
	    while (it.hasNext()) {
	       String key =  (String)it.next();
	       result.add(sortedCache.get(key));
	    }
	    }else {
			Connection connection = DbUtils.getInstance();
			try {
				connection.setAutoCommit(false);
				PreparedStatement  stmt = connection.prepareStatement("SELECT category.id_category, category.text FROM category" + "_" + lang + " ORDER BY id_category");
				ResultSet rs = stmt.executeQuery();
				connection.commit();
				while (rs.next()) { 
					CategoryDb category = new CategoryDb();
					category.setIdCategory(rs.getInt("id_category"));
					category.setText(rs.getString("text"));
					result.add (category);
				}
			}catch (SQLException ex) {
				logger.error("Find all categories End");
			}
		}
		return result;		
 
	}
Merci de vos lumières.