Browse Source

add credis

Andrew Dolgov 8 months ago
parent
commit
dd2240c4f9

+ 8 - 8
daemon/src/org/fox/ttirc/NativeConnectionHandler.java

@@ -105,7 +105,7 @@ public class NativeConnectionHandler extends ConnectionHandler {
 		this.m_conn = m_master.createConnection();
 	}
 
-	public void setM_active(boolean m_active) throws SQLException {
+	public void setActive(boolean m_active) {
 		this.m_active = m_active;
 	}
 
@@ -234,7 +234,7 @@ public class NativeConnectionHandler extends ConnectionHandler {
 			setStatus(Constants.CS_CONNECTING);
 
 			ps = getConnection().prepareStatement("UPDATE ttirc_channels SET " +
-					"m_nicklist = '' WHERE connection_id = ?");
+					"nicklist = '' WHERE connection_id = ?");
 
 			ps.setInt(1, m_connectionId);
 			ps.execute();
@@ -320,7 +320,7 @@ public class NativeConnectionHandler extends ConnectionHandler {
 
 	public void kill() {
 		try {
-			setM_active(false);
+			setActive(false);
 		} catch (SQLException e) {
 			e.printStackTrace();
 		}
@@ -636,12 +636,12 @@ public class NativeConnectionHandler extends ConnectionHandler {
 				m_irc.doAway("Automatically set away: no clients connected.");
 			}
 
-			setM_active(enabled);
+			setActive(enabled);
 		} else {
 			pushMessage("---", "---", "DISCONNECT_USER_IDLE", Constants.MSGT_EVENT);
 
 			logger.info("[" + m_connectionId + "] Disconnecting due to user inactivity.");
-			setM_active(false);
+			setActive(false);
 		}
 
 		ps.close();
@@ -666,7 +666,7 @@ public class NativeConnectionHandler extends ConnectionHandler {
 							Constants.MSGT_EVENT);
 
 					logger.info("Disconnecting from server, connection timeout.");
-					setM_active(false);
+					setActive(false);
 				}
 
 				if (System.currentTimeMillis() - disconnectLastChecked > 3000) {
@@ -834,7 +834,7 @@ public class NativeConnectionHandler extends ConnectionHandler {
 		if (!rs.next()) {
 			ps.close();
 
-			ps = getConnection().prepareStatement("INSERT INTO ttirc_channels (channel, connection_id, chan_type, topic, topic_owner, topic_set, m_nicklist)" +
+			ps = getConnection().prepareStatement("INSERT INTO ttirc_channels (channel, connection_id, chan_type, topic, topic_owner, topic_set, nicklist)" +
 					"VALUES (?, ?, ?, '', '', NOW(), '')");
 			ps.setString(1, channel);
 			ps.setInt(2, m_connectionId);
@@ -916,7 +916,7 @@ public class NativeConnectionHandler extends ConnectionHandler {
 		@Override
 		public void onDisconnected() {
 			try {
-				handler.setM_active(false);
+				handler.setActive(false);
 			} catch (SQLException e) {
 				e.printStackTrace();
 			}

File diff suppressed because it is too large
+ 1452 - 0
lib/credis/Client.php


+ 23 - 0
lib/credis/LICENSE

@@ -0,0 +1,23 @@
+Copyright (c) 2009 Justin Poliey <[email protected]>
+Copyright (c) 2011 Colin Mollenhour <[email protected]>
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.

+ 173 - 0
lib/credis/README.markdown

@@ -0,0 +1,173 @@
+[![Build Status](https://travis-ci.org/colinmollenhour/credis.svg?branch=master)](https://travis-ci.org/colinmollenhour/credis)
+
+# Credis
+
+Credis is a lightweight interface to the [Redis](http://redis.io/) key-value store which wraps the [phpredis](https://github.com/nicolasff/phpredis)
+library when available for better performance. This project was forked from one of the many redisent forks.
+
+## Getting Started
+
+Credis_Client uses methods named the same as Redis commands, and translates return values to the appropriate
+PHP equivalents.
+
+```php
+require 'Credis/Client.php';
+$redis = new Credis_Client('localhost');
+$redis->set('awesome', 'absolutely');
+echo sprintf('Is Credis awesome? %s.\n', $redis->get('awesome'));
+
+// When arrays are given as arguments they are flattened automatically
+$redis->rpush('particles', array('proton','electron','neutron'));
+$particles = $redis->lrange('particles', 0, -1);
+```
+Redis error responses will be wrapped in a CredisException class and thrown.
+
+Credis_Client also supports transparent command renaming. Write code using the original command names and the
+client will send the aliased commands to the server transparently. Specify the renamed commands using a prefix
+for md5, a callable function, individual aliases, or an array map of aliases. See "Redis Security":http://redis.io/topics/security for more info.
+
+## Clustering your servers
+
+Credis also includes a way for developers to fully utilize the scalability of Redis with multiple servers and [consistent hashing](http://en.wikipedia.org/wiki/Consistent_hashing).
+Using the [Credis_Cluster](Cluster.php) class, you can use Credis the same way, except that keys will be hashed across multiple servers.
+Here is how to set up a cluster:
+
+### Basic clustering example
+```php
+<?php
+require 'Credis/Client.php';
+require 'Credis/Cluster.php';
+
+$cluster = new Credis_Cluster(array(
+    array('host' => '127.0.0.1', 'port' => 6379, 'alias'=>'alpha'),
+    array('host' => '127.0.0.1', 'port' => 6380, 'alias'=>'beta')
+));
+$cluster->set('key','value');
+echo "Alpha: ".$cluster->client('alpha')->get('key').PHP_EOL;
+echo "Beta: ".$cluster->client('beta')->get('key').PHP_EOL;
+```
+
+### Explicit definition of replicas
+
+The consistent hashing strategy stores keys on a so called "ring". The position of each key is relative to the position of its target node. The target node that has the closest position will be the selected node for that specific key.
+
+To avoid an uneven distribution of keys (especially on small clusters), it is common to duplicate target nodes. Based on the number of replicas, each target node will exist *n times* on the "ring".
+
+The following example explicitly sets the number of replicas to 5. Both Redis instances will have 5 copies. The default value is 128.
+
+```php
+<?php
+require 'Credis/Client.php';
+require 'Credis/Cluster.php';
+
+$cluster = new Credis_Cluster(
+    array(
+        array('host' => '127.0.0.1', 'port' => 6379, 'alias'=>'alpha'),
+        array('host' => '127.0.0.1', 'port' => 6380, 'alias'=>'beta')
+    ), 5
+);
+$cluster->set('key','value');
+echo "Alpha: ".$cluster->client('alpha')->get('key').PHP_EOL;
+echo "Beta: ".$cluster->client('beta')->get('key').PHP_EOL;
+```
+
+## Master/slave replication
+
+The [Credis_Cluster](Cluster.php) class can also be used for [master/slave replication](http://redis.io/topics/replication).
+Credis_Cluster will automatically perform *read/write splitting* and send the write requests exclusively to the master server.
+Read requests will be handled by all servers unless you set the *write_only* flag to true in the connection string of the master server.
+
+### Redis server settings for master/slave replication
+
+Setting  up master/slave replication is simple and only requires adding the following line to the config of the slave server:
+
+```
+slaveof 127.0.0.1 6379
+```
+
+### Basic master/slave example
+```php
+<?php
+require 'Credis/Client.php';
+require 'Credis/Cluster.php';
+
+$cluster = new Credis_Cluster(array(
+    array('host' => '127.0.0.1', 'port' => 6379, 'alias'=>'master', 'master'=>true),
+    array('host' => '127.0.0.1', 'port' => 6380, 'alias'=>'slave')
+));
+$cluster->set('key','value');
+echo $cluster->get('key').PHP_EOL;
+echo $cluster->client('slave')->get('key').PHP_EOL;
+
+$cluster->client('master')->set('key2','value');
+echo $cluster->client('slave')->get('key2').PHP_EOL;
+```
+
+### No read on master
+
+The following example illustrates how to disable reading on the master server. This will cause the master server only to be used for writing.
+This should only happen when you have enough write calls to create a certain load on the master server. Otherwise this is an inefficient usage of server resources.
+
+```php
+<?php
+require 'Credis/Client.php';
+require 'Credis/Cluster.php';
+
+$cluster = new Credis_Cluster(array(
+    array('host' => '127.0.0.1', 'port' => 6379, 'alias'=>'master', 'master'=>true, 'write_only'=>true),
+    array('host' => '127.0.0.1', 'port' => 6380, 'alias'=>'slave')
+));
+$cluster->set('key','value');
+echo $cluster->get('key').PHP_EOL;
+```
+## Automatic failover with Sentinel
+
+[Redis Sentinel](http://redis.io/topics/sentinel) is a system that can monitor Redis instances. You register master servers and Sentinel automatically detects its slaves.
+
+When a master server dies, Sentinel will make sure one of the slaves is promoted to be the new master. This autofailover mechanism will also demote failed masters to avoid data inconsistency.
+
+The [Credis_Sentinel](Sentinel.php) class interacts with the *Redis Sentinel* instance(s) and acts as a proxy. Sentinel will automatically create [Credis_Cluster](Cluster.php) objects and will set the master and slaves accordingly.
+
+Sentinel uses the same protocol as Redis. In the example below we register the Sentinel server running on port *26379* and assign it to the [Credis_Sentinel](Sentinel.php) object.
+We then ask Sentinel the hostname and port for the master server known as *mymaster*. By calling the *getCluster* method we immediately get a [Credis_Cluster](Cluster.php) object that allows us to perform basic Redis calls.
+
+```php
+<?php
+require 'Credis/Client.php';
+require 'Credis/Cluster.php';
+require 'Credis/Sentinel.php';
+
+$sentinel = new Credis_Sentinel(new Credis_Client('127.0.0.1',26379));
+$masterAddress = $sentinel->getMasterAddressByName('mymaster');
+$cluster = $sentinel->getCluster('mymaster');
+
+echo 'Writing to master: '.$masterAddress[0].' on port '.$masterAddress[1].PHP_EOL;
+$cluster->set('key','value');
+echo $cluster->get('key').PHP_EOL;
+```
+### Additional parameters
+
+Because [Credis_Sentinel](Sentinel.php) will create [Credis_Cluster](Cluster.php) objects using the *"getCluster"* or *"createCluster"* methods, additional parameters can be passed.
+
+First of all there's the *"write_only"* flag. You can also define the selected database and the number of replicas. And finally there's a *"selectRandomSlave"* option.
+
+The *"selectRandomSlave"* flag is used in setups for masters that have multiple slaves. The Credis_Sentinel will either select one random slave to be used when creating the Credis_Cluster object or to pass them all and use the built-in hashing.
+
+The example below shows how to use these 3 options. It selects database 2, sets the number of replicas to 10, it doesn't select a random slave and doesn't allow reading on the master server.
+
+```php
+<?php
+require 'Credis/Client.php';
+require 'Credis/Cluster.php';
+require 'Credis/Sentinel.php';
+
+$sentinel = new Credis_Sentinel(new Credis_Client('127.0.0.1',26379));
+$cluster = $sentinel->getCluster('mymaster',2,10,false,true);
+$cluster->set('key','value');
+echo $cluster->get('key').PHP_EOL;
+```
+
+## About
+
+&copy; 2011 [Colin Mollenhour](http://colin.mollenhour.com)
+&copy; 2009 [Justin Poliey](http://justinpoliey.com)