]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/pgmemcache-perm_cache.sql
LP#1638299: Stamping upgrade scripts for authority infrastructure work
[working/Evergreen.git] / Open-ILS / src / sql / Pg / pgmemcache-perm_cache.sql
1 BEGIN;
2
3 /*
4  * Use pgmemcache and memcached to increase the speed of permission tests
5  * ----------------------------------------------------------------------
6  *
7  * This set of functions allows the use of memcached as a caching mechanism for
8  * permission checks.  It is transparent and optional.  If memcache is not set
9  * up, either by not running or the lack of the pgmemcache postgres addon,
10  * then the default, existing behaviour is preserved and live database queries
11  * are used to test all permissions.
12  *
13  *
14  * On postgres 8.2 and before, pgmemcache 1.1 is required.  For this older
15  * version of pgmemcache, configuration of memcached servers is performed by
16  * stored procs.  Therefore, the installer of this Evergreen addition must
17  * edit the stored proc called permission.old_mc_servers() to initialize the
18  * appropriate set of memcached servers.  For simple, single-database
19  * installations, the default of 'localhost' is most likely the desired
20  * setting.
21  *
22  *
23  * On postgres 8.3 and later, pgmemcache 2.x is required.  In this new
24  * pgmemcache the server configuration is controlled from within the
25  * postgresql.conf file via user-defined variables read by the pgmemcache
26  * intialization routines.  Please see the README for pgmemcache at
27  *
28  *    http://cvs.pgfoundry.org/cgi-bin/cvsweb.cgi/pgmemcache/pgmemcache/README.pgmemcache?rev=1.21&content-type=text/plain
29  *
30  * or in the release tarball that was installed for details on configuration.
31  *
32  *
33  * TODO: Make the cache timeout configurable via a global setting for EG 2.0
34  *
35  */
36
37 CREATE OR REPLACE FUNCTION permission.old_mc_servers() RETURNS BOOL AS $f$
38 BEGIN
39     PERFORM memcache_server_add('localhost', '11211');
40 END;
41 $f$ LANGUAGE PLPGSQL;
42
43 CREATE OR REPLACE FUNCTION permission.mc_init() RETURNS BOOL AS $f$
44 DECLARE
45     old_memcache BOOL;
46 BEGIN
47     old_memcache = FALSE;
48     IF REGEXP_REPLACE(VERSION(),E'^.+?(\\d+\\.\\d+).*?$',E'\\1')::FLOAT < 8.3 THEN
49         old_memcache = TRUE;
50         IF memcache_init() THEN
51             PERFORM permission.old_mc_servers();
52         END IF;
53         -- RAISE NOTICE 'Old postgres, must be old pgmemcache';
54     ELSE
55         BEGIN
56             old_memcache = TRUE; 
57             IF memcache_init() THEN
58                 PERFORM permission.old_mc_servers();
59             END IF;
60             -- RAISE NOTICE 'New postgres, but old pgmemcache';
61         EXCEPTION WHEN OTHERS THEN
62             old_memcache = FALSE;
63         END;
64     END IF;
65
66     IF NOT old_memcache THEN
67         PERFORM current_setting('pgmemcache.default_servers');
68         -- RAISE NOTICE 'New postgres, new pgmemcache';
69     END IF;
70
71     -- no exception, we're good
72     RETURN TRUE;
73 EXCEPTION WHEN OTHERS THEN
74     RETURN FALSE;
75 END;
76 $f$ LANGUAGE PLPGSQL;
77
78 CREATE OR REPLACE FUNCTION permission.set_cached_perm( iusr INT, tperm TEXT, iorg INT, bool_value BOOL, timeout INTERVAL ) RETURNS BOOL AS $f$
79 BEGIN
80     IF permission.mc_init() THEN
81         -- RAISE NOTICE 'Setting perm cache';
82         IF bool_value THEN
83             EXECUTE $$SELECT memcache_set('oils_permcache_$$ || iusr || tperm || iorg || $$', 't',$$ || quote_literal(timeout) || $$::INTERVAL);$$;
84         ELSE
85             EXECUTE $$SELECT memcache_set('oils_permcache_$$ || iusr || tperm || iorg || $$', 'f',$$ || quote_literal(timeout) || $$::INTERVAL);$$;
86         END IF;
87     END IF;
88
89     RETURN bool_value;
90 EXCEPTION WHEN OTHERS THEN
91     RETURN bool_value;
92 END;
93 $f$ LANGUAGE PLPGSQL STABLE;
94
95 CREATE OR REPLACE FUNCTION permission.set_cached_perm( iusr INT, tperm TEXT, iorg INT, bool_value BOOL ) RETURNS BOOL AS $f$
96     SELECT permission.set_cached_perm( $1, $2, $3, $4, '10 minutes'::INTERVAL);
97 $f$ LANGUAGE SQL;
98
99 CREATE OR REPLACE FUNCTION permission.check_cached_perm( iusr INT, tperm TEXT, iorg INT ) RETURNS BOOL AS $f$
100 DECLARE
101     cached_value  RECORD;
102     bool_value    BOOL;
103 BEGIN
104     IF permission.mc_init() THEN
105         -- RAISE NOTICE 'Getting perm from cache';
106         EXECUTE $$SELECT memcache_get('oils_permcache_$$ || iusr || tperm || iorg || $$') AS x;$$ INTO cached_value;
107         bool_value := cached_value.x = 't';
108     END IF;
109
110     RETURN bool_value;
111 EXCEPTION WHEN OTHERS THEN
112     RETURN NULL;
113 END;
114 $f$ LANGUAGE PLPGSQL STABLE;
115
116 CREATE OR REPLACE FUNCTION permission.usr_has_perm ( INT, TEXT, INT ) RETURNS BOOL AS $f$
117     SELECT  CASE
118             WHEN permission.check_cached_perm( $1, $2, $3 ) IS NOT NULL THEN permission.check_cached_perm( $1, $2, $3 )
119             WHEN permission.set_cached_perm($1, $2, $3, permission.usr_has_home_perm( $1, $2, $3 )) THEN TRUE
120             WHEN permission.set_cached_perm($1, $2, $3, permission.usr_has_work_perm( $1, $2, $3 )) THEN TRUE
121             ELSE FALSE
122         END;
123 $f$ LANGUAGE SQL;
124
125 COMMIT;
126